Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying Django Messages Framework Messages

I have been using the Django Messaging Framework to display messages to a user in the template.

I am outputting them to the template like this:

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

This outputs all the messages, errors, warning, success etc. I was just wondering if anyone had any ideas how to display only the error messages something like:

<ul>
    {% for message in messages.errors %}
        <li>{{ message }}</li>
    {% endfor %}
</ul>

The best I have come up with so far is this:

{% if messages %}
    {% for message in messages %}
        {% if forloop.first %}
            {% if message.tags == 'error' %}
                <div class="error">
                    <ul>
            {% endif %}
        {% endif %}

        <li>{{ message }}</li>

        {% if forloop.last %}
                </ul>
            </div>
        {% endif %}
    {% endfor %}
{% endif %}

Any ideas? Thanks in advance.

like image 669
Arif Avatar asked Apr 23 '10 09:04

Arif


1 Answers

Reto's answer works for me in this way

{% for message in messages %}
    {% if 'success' in message.tags %}

        <div class="alert alert-success">
            <a class="close" href="#" data-dismiss="alert">×</a>
            <strong>Success!</strong>

                {{ message }}

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

{% for message in messages %}
    {% if 'error' in message.tags %}
        <div class="alert alert-error">
            <a class="close" href="#" data-dismiss="alert">×</a>
            <strong>Error!</strong>

                {{ message }}

        </div>
    {% endif %}
{% endfor %}
{% for message in messages %}
    {% if 'info' in message.tags %}
        <div class="alert alert-info">
            <a class="close" href="#" data-dismiss="alert">×</a>
            <strong>INFO!</strong>

                {{ message }}

        </div>
    {% endif %}
{% endfor %}
like image 175
Pablo Carpio Avatar answered Oct 05 '22 13:10

Pablo Carpio