[Fixed]-Displaying Django Messages Framework Messages

10👍

You can put an ifequal:

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

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

5👍

A bit of a faff, but you could probably achieve this by adding a custom template context processor (cf. https://docs.djangoproject.com/en/dev/ref/templates/api/ ) — something like

def collect_error_messages(request):
    messages = get_messages(request)
    error_messages = [ m for m in messages if 'error' in m.tags]
    return {'error_messages': error_messages}

then add that to your TEMPLATE_CONTEXT_PROCESSORS list in settings.py, and then in templates you can do:

<ul>
    {% for message in error_messages %}
        <li>{{ message }}</li>
    {% endfor %}
</ul>

You could do a variation on the same to build a dict mapping error level to message, and then iterate through each dict.

👤Ben

5👍

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

1👍

I managed with template tags only:

{% if messages %}
    {% regroup messages by tags as messages %}
    <div id="messages">
    {% for tags in messages %}
        <ul class="{{ tags.grouper }}">
            {% for message in tags.list %}
                <li>{{ message|capfirst }}</li>
            {% endfor %}
        </ul>
    {% endfor %}
    </div>
{% endif %}

The key is the {% regroup %} tag.

This still has an some issues because the tags attribute includes the extra_tags of the message so if you make use of it you will get additional <ul> groups.

In future versions (probably 1.7), there will be a level_tag attribute so that issue will be gone soon.


(As soon as the level_tag attribute is available)

{% if messages %}
    {% regroup messages by level_tag as messages %}
    <div id="messages">
    {% for level in messages %}
        <ul class="{{ level.grouper }}">
            {% for message in level.list %}
                <li>{{ message|capfirst }}</li>
            {% endfor %}
        </ul>
    {% endfor %}
    </div>
{% endif %}

1👍

you can use following to check message tags.

{% if message.tags == "error" %}
your code here
{% endif %}

0👍

Of course you can do it with {% regroup %} tag but you have to use dictsort filter aswell if you want to work properly. So firstly, tags should be sort by name and then group:

{% if messages %}
    {% regroup messages|dictsort:"tags" by tags as message_list  %}

    {% for tags in message_list %}
        <div class="alert alert-{{ tags.grouper }}">
            <div class="container">
                <ul>
                    {% for message in tags.list %}
                        <li>
                              {{ message }}
                        </li>
                    {% endfor %}
                </ul>
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
            </div>
        </div>
    {% endfor %}
{% endif %}

Leave a comment