Last Updated: February 25, 2016
·
2.907K
· 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 nomadblue.com we use a simple template tag to return the proper twitter bootstrap alert class:

@register.assignment_tag
def get_twitter_bootstrap_alert_msg_css_name(tags):
 return 'danger' if tags == 'error' else tags

Usage:

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