亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從模型字段獲取項目

如何從模型字段獲取項目

慕婉清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 關注
  • 86 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號