繁体   English   中英

Django模板外键检查

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM