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>