Flask Message Flashing Flashing With Categories


Set second argument when use flash() in view function:

flash('Something was wrong!', 'error')

In the template, set with_categories=true in get_flashed_messages(), then you get a list of tuples in the form of (message, category), so you can use category as a HTML class.

{% with messages = get_flashed_messages(with_categories=true) %}
  {% if messages %}
    <ul class=flashes>
    {% for category, message in messages %}
      <li class="{{ category }}">{{ message }}</li>
    {% endfor %}
  {% endif %}
{% endwith %}