Flash Messages Between Requests
Show one-time notices after a redirect.
Flash Messages Between Requests is a free Flask Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The One-Time Notice
After saving a form you often want to show Saved! once, then never again. Flask calls this a flash message. ✨
Why Not Just Pass It
The trick is that you usually redirect after a save, and the redirect loses your variables. Flash survives that hop for you.
It Rides the Session
Under the hood, flash tucks the message into the session, so you must have a SECRET_KEY configured for it to work.
Flash a Message
In your view, call flash with the text right before you redirect the user to the next page.
from flask import flash, redirect
flash('Profile saved!')
return redirect('/profile')Read Them in the Template
On the destination page you pull the queued notices with get_flashed_messages and loop over them in Jinja2.
{% for m in get_flashed_messages() %}
<p>{{ m }}</p>
{% endfor %}Shown Once Only
Reading the messages also clears them. Refresh the page and the notice is gone, exactly the behavior you want.
Add a Category
Pass a second argument to tag a message with a category like success or error, so you can style each kind differently.
flash('Wrong password', 'error')Read Categories Too
Ask for categories with with_categories set to true, and each entry becomes a category and message pair.
{% for cat, m in get_flashed_messages(with_categories=true) %}Filter by Category
You can also request only certain kinds using category_filter, handy when one block shows just errors.
get_flashed_messages(category_filter=['error'])Put It in the Layout
Render flashes in your base template so every page can surface notices without repeating the loop everywhere.
Keep Them Short
Flash is for brief, friendly feedback, not long reports. One clear sentence per message keeps the experience clean.
Quick Check
Consider what happens after a template reads the flashed messages.
Recap
You learned that flash queues a one-time message through the session, survives a redirect, and clears once shown. 🎉
Frequently asked questions
Is the “Flash Messages Between Requests” lesson free?
Yes — the full text of “Flash Messages Between Requests” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.
What will I learn in “Flash Messages Between Requests”?
Show one-time notices after a redirect. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flask Academy?
No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Flash Messages Between Requests” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flask Academy lesson?
Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Set and Read the session Dict
- The SECRET_KEY and Signed Cookies
- Set Custom Cookies on a Response
- Flash Messages Between Requests