Flask Message Flashing API
The Flash mechanism is used to display a one-time feedback message on the next request, a typical scenario being "operation successful" or "input error" notifications.
Flask stores messages in the Session and automatically clears them after they are read.
flash function
| Parameter | Type | Default value | Description |
|---|---|---|---|
| message | str | Required | The message text to be displayed |
| category | str | "message" | Message categories, recommended values: message, success, error, warning, info |
get_flashed_messages function
| Parameter | Type | Default value | Description |
|---|---|---|---|
| with_categories | bool | False | When True, returns a list of (category, message) tuples |
| category_filter | iterable | () | Only return messages of the specified category |
| Return Value | Description |
|---|---|
| with_categories=False | Returns a list of message texts: ["msg1", "msg2"] |
| with_categories=True | Returns a list of (category, message) tuples: [("success", "msg1"), ("error", "msg2")] |
Use in templates
get_flashed_messages()They can be called directly in the template without needing to be passed through render_template.
Example
<!-- Display all flash messages -->
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div class="flash-{{ category }}">{{ message }}</div>
{% endfor %}
{% endif %}
{% endwith %}
<!-- Display only error messages -->
{% for msg in get_flashed_messages(category_filter=["error"]) %}
<div class="error">{{ msg }}</div>
{% endfor %}
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div class="flash-{{ category }}">{{ message }}</div>
{% endfor %}
{% endif %}
{% endwith %}
<!-- Display only error messages -->
{% for msg in get_flashed_messages(category_filter=["error"]) %}
<div class="error">{{ msg }}</div>
{% endfor %}
Code Examples
Example
from flask import Flask, flash, get_flashed_messages, redirect, url_for, request
app = Flask(__name__)
app.secret_key = "secret"
@app.route("/post", methods=["GET", "POST"])
def create_post():
if request.method == "POST":
title = request.form.get("title", "").strip()
if not title:
flash("Title cannot be empty", "error")
else:
flash(f"Article '{title}' published successfully!", "success")
return redirect(url_for("create_post"))
return redirect(url_for("create_post"))
# GET request: retrieve and display all messages
messages = get_flashed_messages(with_categories=True)
return render_template("post.html", messages=messages)
app = Flask(__name__)
app.secret_key = "secret"
@app.route("/post", methods=["GET", "POST"])
def create_post():
if request.method == "POST":
title = request.form.get("title", "").strip()
if not title:
flash("Title cannot be empty", "error")
else:
flash(f"Article '{title}' published successfully!", "success")
return redirect(url_for("create_post"))
return redirect(url_for("create_post"))
# GET request: retrieve and display all messages
messages = get_flashed_messages(with_categories=True)
return render_template("post.html", messages=messages)
other extensionsFlash messages are stored in the Session, so the secret key must be setSECRET_KEY. The message is only visible in the next request after it is read, and is then automatically cleared.