{% if user.userprofile.profile %}
<img src="{{ user.userprofile.profile.url }}" />
{% else %}
<img src="something" />
{% endif %}
如果用户没有个人资料图片,那么我想给他们第一个用户的个人资料图片(表示id=1)。
我该怎么做?
在模板的返回语句中传递第一个用户图片网址
在您的 views.py
return render(request, 'your_template_url', {'user': user, 'pic_url': pic_url})
在您的模板中
{% if user.userprofile.profile %}
<img src="{{user.userprofile.profile.url}}" />
{% else %}
<img src="{{pic_url}}" />
{% endif %}