Skip to content

Flash Messages

Flash messages are one-time messages stored in the user’s session.

They’re commonly used after:

  • successful form submission
  • login/logout
  • errors like “invalid password”
diagram a flash message lives in the session until something reads it mermaid
flash writes into the session, so the message survives the redirect that follows it. The template reads it on the next request and the read CONSUMES it -- which is why the message appears exactly once and is gone on reload.
python
from flask import flash, redirect, url_for
 
@app.route("/subscribe", methods=["POST"])
def subscribe():
    # ...do work...
    flash("Subscribed successfully!", "success")
    return redirect(url_for("home"))

The second argument ("success") is a category.

In your base template:

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

Flashing uses sessions, so you need SECRET_KEY configured.

Use flash + redirect (PRG pattern) so the message shows on the GET page.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading