最佳答案
当用户注册我的应用程序时,当他到达个人资料页面时,我收到了这个错误。
The 'image' attribute has no file associated with it.
Exception Type: ValueError
Error during template rendering
In template C:\o\mysite\pet\templates\profile.html, error at line 6
1 <h4>My Profile</h4>
2
3 {% if person %}
4 <ul>
5 <li>Name: {{ person.name }}</li>
6 <br><img src="{{ person.image.url }}">
Traceback Switch back to interactive view
File "C:\o\mysite\pet\views.py" in Profile
71. return render(request,'profile.html',{'board':board ,'person':person})
我认为这个错误发生,因为我的模板需要一个图像,看到他刚刚注册,他不能添加一个图像,除非他去编辑页面,并添加一个页面,然后他可以访问个人资料页面。
我的个人资料 html
<h4>My Profile</h4>
{% if person %}
<ul>
<li>Name: {{ person.name }}</li>
<br><img src="{{ person.image.url }}">
</ul>
{% endif %}
我的个人资料函数在 views.py
def Profile(request):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('world:LoginRequest'))
board = Board.objects.filter(user=request.user)
person = Person.objects.get(user=request.user)
return render(request,'profile.html',{'board':board ,'person':person})
我尝试了这个解决方案,创建了一个2实例的 Person 对象,并在我的模板中使用一个 if 来分隔它们,但是没有成功。
<h4>My Profile</h4>
{% if person %}
<ul>
<li>Name: {{ person.name }}</li>
</ul>
{% endif %}
{% if bob %}
<ul>
<br><img src="{{ bob.image.url }}">
</ul>
我对配置文件函数的解决方案
def Profile(request):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('world:LoginRequest'))
board = Board.objects.filter(user=request.user)
person = Person.objects.get(user=request.user)
bob = Person.objects.get(user=request.user)
return render(request,'profile.html',{'board':board ,'person':person,'bob':bob})
我一直在阅读有关内置模板标签和过滤器的文档,我认为这里的解决方案是使用(和)模板标签,但我似乎不能正确使用它。
如何配置此模板使图片成为一个选项。如果他们没有图片留下它,但显示人名。
谢谢你帮我