无法在模板中显示profile_pic图片
我知道我做的事情很糟糕。
model.py
class UserProfileInfo(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
portfolio_site = models.URLField(blank=True)
profile_pic = models.ImageField(upload_to='basic_app/profile_pics',blank=True)
def __str__(self):
return self.user.username
index.html
{% block body_block %}
<div class="container">
<div class="jumbotron">
<h1>Django Level Five</h1>
{% if user.is_authenticated %}
<h2>Welcome {{ user.username }}!</h2>
<div>
<img src="{{ user.profile_pic.url }}" alt="user-image">
</div>
{% else %}
<h2>Welcome to the site!</h2>
{% for b in users %}
{% endfor %}
{% endif %}
</div>
</div>{% endblock %}
views.py
def index(request):
#commented: userpro = UserProfileInfo()
#commented: var={'userz': userpro}
#commented: return render(request, 'basic_app/index.html',context=var)
return render(request, 'basic_app/index.html')
答案 0 :(得分:0)
用户对象没有profile_pic字段。
尝试使用:
user.userprofileinfo.profile_pic.url