2015-02-10 60 views
0

我得到这个错误,我不知道如何解决它。我最终希望为我的相册添加缩略图,点击后我将能够看到该相册中的所有照片。将模型实例对象添加到模板

我想添加我的数据库中的所有专辑模型对象,并列出具有相同专辑的所有照片。我试图首先列出我的模板上的相册,看看我得到什么值,但我得到这个错误。

回溯:

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response 
    111.      response = wrapped_callback(request, *callback_args, **callback_kwargs) 
File "/Users/bli1/Development/Django/Boothie/home/views.py" in load_home_content 
    78.  return render(request, 'home/home.html', albums) 
File "/Library/Python/2.7/site-packages/django/shortcuts.py" in render 
    48.  return HttpResponse(loader.render_to_string(*args, **kwargs), 
File "/Library/Python/2.7/site-packages/django/template/loader.py" in render_to_string 
    177.  with context_instance.push(dictionary): 
File "/Library/Python/2.7/site-packages/django/template/context.py" in push 
    54.   return ContextDict(self, *args, **kwargs) 
File "/Library/Python/2.7/site-packages/django/template/context.py" in __init__ 
    19.   super(ContextDict, self).__init__(*args, **kwargs) 

Exception Type: TypeError at/
Exception Value: cannot convert dictionary update sequence element #0 to a sequence 

车型:包含在home.html的

class Album(models.Model): 
    title = models.CharField(max_length=50, unique=True) 

class Photo(models.Model): 
    title = models.CharField(max_length=50, blank=True) 
    album = models.ForeignKey(Album) 
    photo = models.ImageField(upload_to=upload_path) 
    upload = models.DateTimeField(auto_now_add=True) 

view.py

def load_home_content(request): 
    albums = Album.objects.all() 

    if request.method == "POST": 
     form = ContactForm(request.POST) 
     if form.is_valid(): 
      # after is_valid(), the validated form data willbe in the form.cleaned_data dictionary. 
      # Data will have been nicely converted into Python types 
      email = form.cleaned_data['email'] 
      albums = get_albums() 
      if User.objects.filter(email=email).exists(): 
       print("email exists") 
       existing_user = User.objects.filter(email=email) 
       message = form.cleaned_date['message'] 
       ContactRequest(message=message, user=existing_user, email=email).save() 
       return render(request, 'home/home.html', albums) 
      else: 
       print("email/user does not exist") 
       first_name = form.cleaned_data['first_name'] 
       last_name = form.cleaned_data['last_name'] 
       phone_number = form.cleaned_data['phone_number'] 

       new_user = User(first_name=first_name, last_name=last_name, phone_number=phone_number, email=email) 
       new_user.save() 
       message = form.cleaned_data['message'] 
       time = datetime.now() 
       contact_request = ContactRequest(message=message, user=new_user, datetime_created=time) 
       contact_request.save() 
       return render(request, 'home/home.html', albums) 
     else: 
      print "form invalid" 
      return render(request, 'home/home.html', albums) 
    return render(request, 'home/home.html', albums) 

portfolio.html

<div class="col-md-4 col-sm-6 portfolio-item"> 
    <a href="#" class="portfolio-link"> 
     <div class="portfolio-hover"> 
     {{ albums }} 
     </div> 
     <img src="{{ STATIC_URL }}home/images/roundicons.png"> 
    </a> 
    </div> 

回答

2

渲染函数的第三个参数必须是字典。

return render(request, 'home/home.html', {'albums': albums}) 

出于某种原因,你在你的观点得到了同样的错误的代码几次。