【发布时间】:2021-11-17 06:51:18
【问题描述】:
我有以下模型用于“存档”帖子(即,如果它是由用户为帖子创建的,则该帖子现在对该用户隐藏)
models.py
class ArchivedFlag(models.Model):
group = models.ForeignKey(Group,
on_delete=models.CASCADE,
related_name='archived_commits')
post = models.ForeignKey(Commit,
on_delete=models.CASCADE,
related_name='archived_flag')
user = models.ForeignKey(User,
on_delete=models.CASCADE,
related_name='archives')
在一个特定的模板中,我想要基于当前用户组和正在检查的当前帖子是否存在 ArchivedFlag 的逻辑。
模板.html
{% for p in posts %}
<form action="{% url 'archive_posts' p.oid %}" method="post">
{% csrf_token %}
<input type="hidden" name="next" value="{{ request.get_full_path }}">
{% if <...an archived flag exists with post==post and group==user.group...> %}
<...Do stuff for archived post...>
<button type="submit", name="p_oid", value="{{ p.oid }}", class="btn btn-primary btn-sm">Restore</button>
{% else %}
<...do stuff for unarchived post...>
<button type="submit", name="p_oid", value="{{ p.oid }}", class="btn btn-primary btn-sm">Archive</button>
{% endif %}
</form>
{% endfor %}
有没有办法在 django 模板中做到这一点?我在模板中找不到任何关于过滤的信息,所以这可能是不可能的。
【问题讨论】:
标签: html django django-views django-templates django-template-filters