Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将模型实例对象添加到模板_Python_Django - Fatal编程技术网

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

Python 将模型实例对象添加到模板,python,django,Python,Django,我得到这个错误,我不知道如何修复它。我最终想为我的相册添加一个缩略图,点击后我将能够看到相册中的所有照片 我试图在数据库中添加所有相册模型对象,并列出同一相册中的所有照片。我试图首先列出我的模板上的相册,看看我得到了什么值,但我得到了这个错误 回溯: File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response 111. response =

我得到这个错误,我不知道如何修复它。我最终想为我的相册添加一个缩略图,点击后我将能够看到相册中的所有照片

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

回溯:

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)
home.html中包含的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>

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

return render(request, 'home/home.html', {'albums': albums})
由于某种原因,在您的视图中多次出现相同的错误代码