Il framework dei messaggi
Mostrare agli utenti notifiche temporanee di successo ed errore
Il framework dei messaggi è una lezione Django Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Django Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Django Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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. 🍪
Domande Frequenti
La lezione «Il framework dei messaggi» è gratuita?
Sì — il testo completo di «Il framework dei messaggi» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Django Academy, passa a CoddyKit PRO. Il corso Django Academy include 4 lezioni in totale.
Cosa imparerò in «Il framework dei messaggi»?
Mostrare agli utenti notifiche temporanee di successo ed errore Eserciti Django Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Django Academy?
Non è richiesta alcuna esperienza precedente. Django Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.
Quanto tempo richiede la lezione «Il framework dei messaggi»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Django Academy?
Sì. Ogni lezione Django Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Il framework delle sessioni
- Leggere e scrivere request.session
- Il framework dei messaggi
- Cookie o sessioni