djangodjango-templatesdjango-messages

Django message template tag checking


I want to check whether the message tag are INFO ?

How to check that in the template file?

I tried this but it didn't work:

{% if messages %}
    <ul>
        {% for message in messages %}
            <li>
                {% if message.tag == "INFO" %}
                    {{ message }}
                {% endif %}
            </li>
        {% endfor %}
    </ul>
{% endif %}

I even tried other combinations like message.tag.info == "INFO" etc but they didn't seem to work. What is the correct way of checking it?


Solution

  • Using bootstrap

    {% block messages %}
        {% if messages %}
            {% for message in messages %}
                <div class="alert alert-{{ message.tags }}">  <!-- singular -->
                    <a class="close" data-dismiss="alert">×</a>
                    {{ message|safe }}
                </div>
            {% endfor %}
        {% endif %}
    {% endblock %}
    

    So it's {{ message.tags }} you're looking for