How to Use Flash Messages in Flask: Simple Guide
In Flask, use the
flash() function to store a message that you want to show to the user on the next page load. Then, in your template, use get_flashed_messages() to retrieve and display these messages. This helps show notifications like success or error alerts after actions.Syntax
Use flash(message, category) to add a message to the next request. The message is the text to show, and category is optional to label the message type (like 'error' or 'success'). In your template, call get_flashed_messages(with_categories=True) to get a list of messages with their categories.
python
from flask import flash, get_flashed_messages # Add a flash message flash('Your changes were saved.', 'success') # In template, get messages messages = get_flashed_messages(with_categories=True) for category, message in messages: print(f'{category}: {message}')
Output
success: Your changes were saved.
Example
This example shows a simple Flask app that flashes a message after a form submission and displays it on the home page.
python
from flask import Flask, render_template_string, request, redirect, url_for, flash, get_flashed_messages app = Flask(__name__) app.secret_key = 'secret123' # Needed for flashing messages TEMPLATE = ''' <!doctype html> <title>Flash Example</title> {% with messages = get_flashed_messages(with_categories=True) %} {% if messages %} <ul> {% for category, message in messages %} <li><strong>{{ category.title() }}:</strong> {{ message }}</li> {% endfor %} </ul> {% endif %} {% endwith %} <form method="post" action="/submit"> <input type="text" name="name" placeholder="Enter your name" required> <button type="submit">Submit</button> </form> ''' @app.route('/') def index(): return render_template_string(TEMPLATE) @app.route('/submit', methods=['POST']) def submit(): name = request.form.get('name') flash(f'Thank you, {name}!', 'success') return redirect(url_for('index')) if __name__ == '__main__': app.run(debug=True)
Output
When you submit the form, the page reloads and shows: "Success: Thank you, [name]!" above the form.
Common Pitfalls
- Not setting
app.secret_keycauses flash messages to fail silently because Flask needs it to keep session data. - Forgetting to call
get_flashed_messages()in the template means messages won't show. - Not redirecting after flashing can cause messages to appear multiple times if the page is refreshed.
python
from flask import Flask, flash, render_template_string app = Flask(__name__) # app.secret_key = 'secret' # Missing secret key causes flash to fail @app.route('/') def index(): flash('Hello!') # Missing redirect here means message may show repeatedly return render_template_string(''' {% for message in get_flashed_messages() %} <p>{{ message }}</p> {% endfor %} ''')
Quick Reference
- flash(message, category=None): Add a message to show on next request.
- get_flashed_messages(with_categories=False): Retrieve flashed messages; use
with_categories=Trueto get (category, message) pairs. - app.secret_key: Must be set for flashing to work.
- Always redirect after flashing to avoid duplicate messages on refresh.
Key Takeaways
Always set app.secret_key before using flash messages in Flask.
Use flash() to add messages and get_flashed_messages() in templates to display them.
Redirect after flashing to prevent messages from showing multiple times on refresh.
Use message categories to style or differentiate message types easily.
Flash messages are stored in the session and last only for the next request.