Messages Framework
成功やエラーの通知をユーザーに一時表示します
「Messages Framework」はCoddyKit上の無料Django Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDjango Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Django Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
What Flash Messages Are
After an action you often want a one-time notice like Saved! The messages framework shows it on the next page, then forgets it. ✨
Already Set Up
New projects include the messages app plus its middleware and context processor, so you can start flashing notices right away.
Adding a Message
In a view, call messages.add_message with the request, a level, and your text to queue a notice for the visitor.
from django.contrib import messages
messages.add_message(request, messages.SUCCESS, 'Saved!')Handy Shortcuts
Skip the level argument with helpers like messages.success. They read clearly and cover the levels you use most often.
messages.success(request, 'Profile updated.')The Message Levels
Five built-in levels exist: DEBUG, INFO, SUCCESS, WARNING, and ERROR. Each maps to a CSS tag you can style differently.
Warnings and Errors
Use messages.error when something fails so the visitor sees a clear, red-styled notice instead of a silent failure.
messages.error(request, 'Could not delete that item.')They Survive Redirects
Messages are stored until displayed, so they survive a redirect. That fits the post-then-redirect pattern after a form submit perfectly.
Showing Them in Templates
The context processor exposes a messages variable. Loop over it in your base template so every page can show notices.
{% for m in messages %}
<li>{{ m }}</li>
{% endfor %}Styling by Level
Each message carries message.tags, like success or error. Drop that into a class to color notices with your CSS.
<li class="{{ m.tags }}">{{ m }}</li>Read Once, Then Gone
Iterating the messages variable marks them read. They will not reappear on the next request, which is exactly the flash behavior you want.
Where They Are Stored
By default messages use the FallbackStorage, which tries the cookie first and spills into the session for longer notices.
Quick Check
What happens to a message once a template loops over it?
Recap
You learned the messages framework: add notices with levels, survive redirects, render them in your base template, and they vanish after one view. Cookies versus sessions is next. 🍪
よくある質問
「Messages Framework」レッスンは無料ですか?
はい。「Messages Framework」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Django Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Django Academyコースには全4レッスンが含まれています。
「Messages Framework」で何を学びますか?
成功やエラーの通知をユーザーに一時表示します ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Django Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDjango Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「Messages Framework」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDjango Academyレッスンでコードを書いて実行できますか?
はい。すべてのDjango Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Session Framework
- request.sessionの読み書き
- Messages Framework
- CookiesとSessionsの比較