springboot怎么把数据传给前端:springboot 如何引入前端 bootstrap?
这种问题网上一搜一大堆,你可以具体找一篇文章试试,遇到问题可以针对相关问题去提问。springboot通过jar包方式引入bootstrap_个人文章 - SegmentFault 思否 这不是查查就
顺晟科技
2022-10-18 13:43:07
35
我有以下模型,用于";存档";帖子(即,如果它是由用户为帖子创建的,则该帖子现在对该用户隐藏)
模型.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模板中做到这一点?我在模板中找不到任何关于过滤的信息,所以这可能是不可能的。
顺晟科技:
你不能在Django模板中过滤/查询。
但是,如果你给你的提交模型一个属性模型方法,比如_存档,你可以在条件中使用它。
模型方法的文档在这里:https://docs.djangoproject.com/en/3.2/topics/db/models/#model-methods
因此,您的提交模型可以有一个返回布尔值的属性,如下所示:
@property
def is_archived(self):
return <however you know it's archived>
然后你可以在你的问题中使用这样的句型:
{%for post in posts %}
{% if post.is_archived %}
do a thing
{% else %}
do a different thing
{% endif %}
{% endfor %}
05
2022-12
02
2022-12
02
2022-12
29
2022-11
29
2022-11
24
2022-11