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.
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 %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With