[英]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.