将模型实例对象添加到模板

时间:2015-02-10 23:17:30

标签: python django

我收到此错误,我不确定如何修复它。我最终想为我的专辑添加缩略图,点击后我就能看到该专辑中的所有照片。

我正在尝试在我的数据库中添加所有相册模型对象,并列出所有相同相册的照片。我试图先在我的模板上列出相册,看看我得到了什么值,但是我收到了这个错误。

回溯:

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

模型:

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包含在home.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>

1 个答案:

答案 0 :(得分:2)

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

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

出于某种原因,您在视图中多次使用相同的错误代码。