Last Updated: February 25, 2016
· nabucosound

Django messages and twitter bootstrap 3 in templates

Unfortunately some alert names in twitter bootstrap o not match with django error messages, so here at we use a simple template tag to return the proper twitter bootstrap alert class:

def get_twitter_bootstrap_alert_msg_css_name(tags):
    return 'danger' if tags == 'error' else tags


<ul class="messages">
  {% for message in messages %}
    {% get_twitter_bootstrap_alert_msg_css_name message.tags as alert_tag %}
    <li class="alert alert-{{ alert_tag }}">{{ message }}</li>
  {% endfor %}