← Back to Django Course | Chapter 7: Django Forms | Lesson 11 of 11

The Django Messages Framework

The messages framework is Django's way of leaving a short sticky note for the user, like 'Saved!', that shows once and then disappears.

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.

markup
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.

markup
{% 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.

markup
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. #}
Common Mistakes
  1. Forgetting 'django.contrib.messages' isn't in INSTALLED_APPS by default removal, and MessageMiddleware missing from MIDDLEWARE, causing messages to silently not appear.
  2. Not looping over {% for message in messages %} in the base template, so messages are created but never rendered.
  3. 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.
Chapter Summary
  • 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.