Flask Message Flashing Flashing With Categories

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

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


Got any Flask Question?