Flash Messaging System & User Feedback UI
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
- 1In your Python route, call
flash(message, category):
- Flask cryptographically signs and stores the message in the client's session cookie.
- 1The user is redirected (
return redirect(...)). - 2In the destination template, call
get_flashed_messages():
- The messages are extracted, rendered on the page, and immediately purged from the session.
- 1If the user refreshes the page, the messages are gone!
2. Flashing Messages in Route Handlers
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:
4. Filtering Messages by Category
In specific modular templates, you may want to extract only messages of a single category:
Practice Quiz
get_flashed_messages() to receive tuples of (category, message)?get_flashed_messages() during page rendering?Flask-SQLAlchemy Setup & Engine Configuration
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Custom Form Validators & Secure File Uploads | Flask-SQLAlchemy Setup & Engine Configuration |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.