The Django Messages Framework
Adding a Success Message
Call messages.success() in a view right before redirecting, and it will appear once on the next page the user sees.
Example: Adding a Success Message
Call messages.success() in a view right before redirecting, and it will appear once on the next page the user sees.
from django.contrib import messages
from django.shortcuts import redirect
def save_article(request):
# ... form saved above ...
messages.success(request, 'Article saved successfully.')
return redirect('article-list')
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Displaying Messages in a Template
Loop over the messages context variable, which Django adds automatically via a context processor, to render each queued message.
Example: Displaying Messages in a Template
Loop over the messages context variable, which Django adds automatically via a context processor, to render each queued message.
{% for message in messages %}
<div class="alert">{{ message }}</div>
{% endfor %}
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Message Levels
messages.success, messages.error, messages.warning, and messages.info tag each message so templates can style them differently.
Example: Message Levels
messages.success, messages.error, messages.warning, and messages.info tag each message so templates can style them differently.
messages.error(request, 'Something went wrong while saving.')
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Forgetting 'django.contrib.messages' isn't in INSTALLED_APPS by default removal, and MessageMiddleware missing from MIDDLEWARE, causing messages to silently not appear.
- Not looping over {% for message in messages %} in the base template, so messages are created but never rendered.
- Calling messages.success() before redirecting but expecting it to show on the current page instead of the next one — messages persist across the next request only.
- django.contrib.messages lets a view queue a one-time notification (success, error, info, warning) for the next page render.
- Messages are stored in the session or cookies and are cleared automatically after being displayed once.
- The base template must loop over the {{ messages }} context variable to actually show them.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: