gpt4 book ai didi

python - Django 模板外键检查

转载 作者:行者123 更新时间:2023-12-01 02:55:59 25 4
gpt4 key购买 nike

模型中:

class Example
text

class Share
example (foreign_key)
user (foreign_key)

View 中:

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 share %} 的内容?或者您有更好的想法吗?

最佳答案

在模板中:

{% 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')
...

希望这有帮助。

关于python - Django 模板外键检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44221469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com