views:

126

answers:

1

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.

A: 

You can put an ifequal:

<ul>
    {% for message in messages.errors %}
        {% ifequal message.tags 'error' %}<li>{{ message }}</li>{% endifequal %}
    {% endfor %}
</ul>

The mapping of message level to message tag can be configured with MESSAGE_TAGS.

aeby
Thanks for your answer but that means that I have to iterate through the errors several times to output all the errors and then all the success messages. I wanted a way to grab all the errors by themselves.
Arif