MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Flash Messaging System & User Feedback UI0%
Study Materials
FlaskProgress 0%
1.1What is Flask & WSGI Architecture1.2Virtual Environments & First Flask Application1.3Flask CLI & Environment Configuration
4.1Flask-WTF Setup & CSRF Token Protection4.2Form Classes & Built-In Field Validators4.3Custom Form Validators & Secure File Uploads4.4Flash Messaging System & User Feedback UI
HomeTutorialsFlaskFlash Messaging System & User Feedback UI
Flask-SQLAlchemy Setup & Engine Configuration

Flash Messaging System & User Feedback UI

Beginner12 min read•Updated: 2026-09-16•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

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!
1[ POST /profile ] ---> flash("Profile updated!", "success") ---> 302 Redirect
2 |
3 v
4[ GET /dashboard ] <-- get_flashed_messages() renders alert <--- (Cookie Cleared)

2. Flashing Messages in Route Handlers

1from flask import Flask, flash, redirect, url_for, render_template
2 
3app = Flask(__name__)
4app.config["SECRET_KEY"] = "super-secret-session-key"
5 
6@app.route("/update-settings", methods=["POST"])
7def update_settings():
8 # Perform business logic...
9 success = True
10
11 if success:
12 # Category allows styling (success, danger, warning, info)
13 flash("Your profile settings were saved successfully!", "success")
14 flash("An email confirmation has been dispatched.", "info")
15 else:
16 flash("Could not update settings. Please verify your inputs.", "danger")
17
18 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:

1<!-- templates/base.html -->
2<main class="container mx-auto px-4 py-6">
3 
4 <!-- Flash Message Notification Container -->
5 {% with messages = get_flashed_messages(with_categories=true) %}
6 {% if messages %}
7 <div class="space-y-3 mb-6">
8 {% for category, message in messages %}
9 <div class="p-4 rounded-xl border flex items-center justify-between
10 {% if category == 'success' %}
11 bg-emerald-50 border-emerald-200 text-emerald-800
12 {% elif category == 'danger' or category == 'error' %}
13 bg-red-50 border-red-200 text-red-800
14 {% elif category == 'warning' %}
15 bg-amber-50 border-amber-200 text-amber-800
16 {% else %}
17 bg-blue-50 border-blue-200 text-blue-800
18 {% endif %}
19 ">
20 <span class="text-sm font-medium">{{ message }}</span>
21 <button onclick="this.parentElement.remove()" class="text-xs font-bold opacity-60 hover:opacity-100">
22 &times;
23 </button>
24 </div>
25 {% endfor %}
26 </div>
27 {% endif %}
28 {% endwith %}
29 
30 {% block content %}{% endblock %}
31</main>

4. Filtering Messages by Category

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

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

Interactive Quiz• Knowledge Check

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.

Related Lessons

Previous LessonNext Lesson
Custom Form Validators & Secure File UploadsFlask-SQLAlchemy Setup & Engine Configuration

Practice Quiz

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

Mentor-Led Practical Training

Master Flask with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext