我是Django的初学者,如何从Profile Model获取profile_pic并将其连接到FriendRequest模型中的“ to_user”字段。
我能够通过以下方式在“ to_user”字段中获取用户名:
{% for data in sent_friend_request %}
{{ data.to_user.username }}
{% endfor %}
如何获取每个用户的profile_pic?
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
profile_pic = models.ImageField()
class FriendRequest(models.Model):
to_user = models.ForeignKey(settings.AUTH_USER_MODEL)
from_user = models.ForeignKey(settings.AUTH_USER_MODEL)
def following_view(request, username):
sent_friend_request = FriendRequest.objects.filter(from_user__username=username)
context = {'sent_friend_request':sent_friend_request}
{% if data.to_user.profile_pic %}
<img src="{{ data.to_user.profile_pic }}">
{% endif %}
答案 0 :(得分:1)
您可以这样做:
{% if data.to_user.profile.profile_pic %}
<img src="{{ data.to_user.profile.profile_pic.url }}">
{% endif %}