Django Messages and Bootstrap 3

28 Feb · by Tim Kamanin · 1 min read

To make Django messages look nice in your bootstrap 3 template you need two things:

1. Create a custom template tag to define css name for a message:

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

2. Add this snippet to your base.html

{% if messages %}
  <ul class="list-unstyled messages">
  {% for message in messages %}
    {% get_bootstrap_alert_msg_css_name message.tags as alert_tag %}
      <li class="alert alert-{{ alert_tag }}">{{ message }}</li>
   {% endfor %}
  </ul>
{% endif %}

Comments

Required for comment verification



Dmitry

Or you could simply add following snippet to settings:

MESSAGE_TAGS = {
'messages.ERROR': 'danger'
}
Reply · 7 months, 2 weeks ago