Django Messages and Bootstrap 3

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:

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

2. Add this snippet to your base.html

1
2
3
4
5
6
7
8
  1. {% if messages %}
  2. <ul class="list-unstyled messages">
  3. {% for message in messages %}
  4. {% get_bootstrap_alert_msg_css_name message.tags as alert_tag %}
  5. <li class="alert alert-{{ alert_tag }}">{{ message }}</li>
  6. {% endfor %}
  7. </ul>
  8. {% endif %}