Flash Messages (Flask)

Flash messages in Flask are one-time notifications, like 'Saved successfully', shown to the user on the next page they see. You queue one with flash() and display queued messages in templates with get_flashed_messages().

Flask

Flash messages solve a specific UX need: after an action that ends in a redirect (such as saving a form), you want to show feedback on the destination page. flash('Saved!') stores the message in the session, and it is automatically removed once displayed.

In the template you loop over get_flashed_messages() to render them, and you can pass a category (flash('Bad input', 'error')) to style different message types differently using with_categories=True.

Because flash uses the session, it requires a configured secret_key. The messages persist exactly until the next request that reads them, which is why they survive a redirect but do not stick around afterward.

Syntax

from flask import flash
flash("Saved!", "success")

Examples

Python

from flask import flash, redirect, url_for

@app.route("/save", methods=["POST"])
def save():
    flash("Your changes were saved", "success")
    return redirect(url_for("index"))

# template:
# {% for cat, msg in get_flashed_messages(with_categories=true) %}
#   <p class="{{ cat }}">{{ msg }}</p>
# {% endfor %}

Learn it in a lesson

See also

Back to the Dictionary