[英]Django Template Foreign Key Check
在模型中:
class Example
text
class Share
example (foreign_key)
user (foreign_key)
鑒於:
def page:
items = Example.objects.get(user=user)
user_shares = Example.objects.get(user=user)
return render(page.html, {'items': items, 'user_shares': user_shares})
在模板中,我可以按行顯示項目。 但是對於共享按鈕,我想添加其他按鈕。 如何在for循環中使用類似 {% if item in shares %}
? 還是您有更好的主意 ?
在模板中:
{% for item in items %}
<td>{{item.text}}</td>
{%if item.shares.count > 0 %}
<td><!-- additional buttons here --></td>
{% endif %}
{% endfor %}
您需要在Example
模型中修改ForeignKey
:
class Share(models.Model):
example = models.ForeignKey(Example, related_name='shares')
...
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.