Flash Messaging System & User Feedback UI0%

Flash Messaging System & User Feedback UI

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Flash Messaging System & User Feedback UI

Web applications frequently redirect users following state-changing operations (such as login, registration, or profile updates) according to the Post/Redirect/Get (PRG) pattern. Because HTTP is stateless, transmitting feedback messages across redirects requires session storage.

Flask provides a lightweight, built-in mechanism called Flashing to record a message in the session and consume it on the next rendered page.


1. How Flask Flash Messaging Works

  1. 1
    In your Python route, call flash(message, category):
  • Flask cryptographically signs and stores the message in the client's session cookie.
  1. 1
    The user is redirected (return redirect(...)).
  2. 2
    In the destination template, call get_flashed_messages():
  • The messages are extracted, rendered on the page, and immediately purged from the session.
  1. 1
    If the user refreshes the page, the messages are gone!
Output
[ POST /profile ] ---> flash("Profile updated!", "success") ---> 302 Redirect
|
v
[ GET /dashboard ] <-- get_flashed_messages() renders alert <--- (Cookie Cleared)

2. Flashing Messages in Route Handlers

Python
from flask import Flask, flash, redirect, url_for, render_template
 
app = Flask(__name__)
app.config["SECRET_KEY"] = "super-secret-session-key"
 
@app.route("/update-settings", methods=["POST"])
def update_settings():
# Perform business logic...
success = True
if success:
# Category allows styling (success, danger, warning, info)
flash("Your profile settings were saved successfully!", "success")
flash("An email confirmation has been dispatched.", "info")
else:
flash("Could not update settings. Please verify your inputs.", "danger")
return redirect(url_for("dashboard"))

3. Rendering Flashed Messages in Jinja2

Use get_flashed_messages(with_categories=True) in your master base.html layout so messages render consistently across every page on your site:

HTML
<!-- templates/base.html -->
<main class="container mx-auto px-4 py-6">
 
<!-- Flash Message Notification Container -->
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
<div class="space-y-3 mb-6">
{% for category, message in messages %}
<div class="p-4 rounded-xl border flex items-center justify-between
{% if category == 'success' %}
bg-emerald-50 border-emerald-200 text-emerald-800
{% elif category == 'danger' or category == 'error' %}
bg-red-50 border-red-200 text-red-800
{% elif category == 'warning' %}
bg-amber-50 border-amber-200 text-amber-800
{% else %}
bg-blue-50 border-blue-200 text-blue-800
{% endif %}
">
<span class="text-sm font-medium">{{ message }}</span>
<button onclick="this.parentElement.remove()" class="text-xs font-bold opacity-60 hover:opacity-100">
&times;
</button>
</div>
{% endfor %}
</div>
{% endif %}
{% endwith %}
 
{% block content %}{% endblock %}
</main>

4. Filtering Messages by Category

In specific modular templates, you may want to extract only messages of a single category:

HTML
<!-- Extract only error messages -->
{% for message in get_flashed_messages(category_filter=["danger", "error"]) %}
<div class="alert-box error">{{ message }}</div>
{% endfor %}

Interactive Quiz

Practice Quiz

Question 1
Where does Flask store flashed messages between the initial request and the subsequent redirect?
Question 2
What parameter must be passed to get_flashed_messages() to receive tuples of (category, message)?
Question 3
What happens to flashed messages after they are read by get_flashed_messages() during page rendering?
Question 4
Why is the Post/Redirect/Get (PRG) pattern combined with flash messaging considered a web design best practice?
Question 5
How do you filter flashed messages to display only "success" alerts in a specific template?
Next Lesson

Flask-SQLAlchemy Setup & Engine Configuration

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext