Django Messages - message tags for CSS classes


Django tip:

You can change Django message tags so they correspond to the CSS class you want to use for displaying the flash message.

This makes it easy to combine Django messages with a CSS framework (e.g., Bootstrap).

For example, this is how you can use Bootstrap with Django messages:

# settings.py
from django.contrib.messages import constants as messages

MESSAGE_TAGS = {
        messages.DEBUG: 'alert-secondary',
        messages.INFO: 'alert-info',
        messages.SUCCESS: 'alert-success',
        messages.WARNING: 'alert-warning',
        messages.ERROR: 'alert-danger',
}

# template
{% for message in messages %}
    <div class="alert {{ message.tags }}" role="alert">
        {{ message }}
    </div>
{% endfor %}