3

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

如何排除帐户登录和注销消息?目前,登录后登陆页面显示 Successfully signed in as 'username'。我不希望显示此消息,但应显示所有其他成功消息。我尝试的内容如下所示。我尝试使用逻辑来查找消息中是否包含单词"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"在其中的消息吗?

4

1 回答 1

4

使用safe过滤器将该message对象属性转换为实际字符串并进行比较

--- Your code ---
{% if "signed" in message|safe %} 
  # Don't display anything
{% else %}
--- Your remaining code ---
于 2017-07-21T06:38:02.610 回答