为了账号安全,请及时绑定邮箱和手机立即绑定

如何从模型字段获取项目

如何从模型字段获取项目

慕婉清6462132 2022-09-06 17:56:06
我是Django初学者,如何从配置文件模型中获取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 %} 
查看完整描述

1 回答

?
郎朗坤

TA贡献1921条经验 获得超9个赞

你可以这样做:


{% if data.to_user.profile.profile_pic %} 


<img src="{{ data.to_user.profile.profile_pic.url }}">


{% endif %}


查看完整回答
反对 回复 2022-09-06
  • 1 回答
  • 0 关注
  • 77 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号