gpt4 book ai didi

Django 消息,如何隐藏特定消息

转载 作者:行者123 更新时间:2023-12-04 02:03:46 29 4
gpt4 key购买 nike

我正在使用 Django 的消息框架来指示成功的操作和失败的操作。

如何排除帐户登录和注销消息?目前,登录后登陆页面显示
已成功登录为“用户名”
。我不希望显示此消息,但应显示所有其他成功消息。我的尝试如下所示。我尝试使用逻辑来查找消息中是否包含单词 "signed"。如果是,则不要显示它。然而,那是行不通的。

{% if messages %}

<div class="db-section">
<ul class="messages">
{% for message in messages %}

{% if "signed" in message %}
# Don't display anything
{% else %}

<div class="alert alert-error">

<strong style="color:{% if 'success' in message.tags %}green{% else %} red {% endif %};padding-bottom:10px;">{{ message }}</strong>

</div>

{% endif %}

{% endfor %}
</ul>
</div>

{% endif %}

有人可以解释为什么上面的代码仍然显示甚至包含 "signed" 的消息吗?

最佳答案

使用 <a href="https://docs.djangoproject.com/en/1.11/ref/templates/builtins/#std:templatefilter-safe" rel="noreferrer noopener nofollow">safe</a>过滤以转换 message对象属性到实际字符串并比较

--- Your code ---
{% if "signed" in message|safe %}
# Don't display anything
{% else %}
--- Your remaining code ---

关于Django 消息,如何隐藏特定消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45225384/

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