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.
- What method attributes are used in Django?
- How to cancel a delete in django signal
- Django : Formset as form field
- Is there any list of blog engines, written in Django?
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 %}
- Setting a session variable in django tests
- Django & the "TemplateDoesNotExist" error
- Image Conversion โ Cannot write mode RGBA as JPEG
- Django form with just a BooleanField
- Login_required decorator in django
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">×</button>
</div>
</div>
{% endfor %}
{% endif %}
- How to use full_clean() for data validation before saving in Django 1.5 gracefully?
- What Django TEST_RUNNER supports xunit xml and logging capture?
- Pycharm (Python IDE) doesn't auto complete Django modules