Search code examples
djangodjango-templatesjinja2django-messages

Django Messages, How to Hide Specific Ones


I am using Django's messages framework to indicate successful actions and failed actions.

How can I exclude account sign in and sign out messages? Currently, landing on a page after signing in displays Successfully signed in as 'username'. I do not want this message to be displayed, but all other success messages should be displayed. What I attempted is shown below. I tried using logic to find if the message had the word "signed" in it. If it did, do not display it. That however is not working though.

{% 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 %} 

Could someone possibly explain why the above code is still displaying messages that even contain "signed" in it?


Solution

  • Use safe filter to convert that message object attribute to actual string and compare

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