リクエスト間でフラッシュメッセージを渡す
リダイレクト後に一度だけ通知を表示します
「リクエスト間でフラッシュメッセージを渡す」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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. 🎉
よくある質問
「リクエスト間でフラッシュメッセージを渡す」レッスンは無料ですか?
はい。「リクエスト間でフラッシュメッセージを渡す」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「リクエスト間でフラッシュメッセージを渡す」で何を学びますか?
リダイレクト後に一度だけ通知を表示します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「リクエスト間でフラッシュメッセージを渡す」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- session辞書を設定して読み取る
- SECRET_KEYと署名付きCookie
- レスポンスにカスタムCookieを設定する
- リクエスト間でフラッシュメッセージを渡す