Django provides a built-in messages framework that allows you to display temporary notifications to users after an action is performed.
Messages are useful for showing information such as successful form submissions, login status, warnings, errors, and other short notifications.
The Django messages framework provides a simple way to send notification messages from a view to a template.
For example, after creating a student record, you can display:
Student added successfully!
Messages provide feedback to users after an action.
Django's messages framework is provided by:
django.contrib.messages
A standard Django project normally includes it in
INSTALLED_APPS.
INSTALLED_APPS = [
"django.contrib.messages",
]
The messages framework requires its middleware to be enabled.
MIDDLEWARE = [
"django.contrib.sessions.middleware.SessionMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
]
The messages middleware works with Django's request and session handling.
Django normally includes a messages context processor in the template configuration.
TEMPLATES = [
{
"OPTIONS": {
"context_processors": [
"django.contrib.messages.context_processors.messages",
],
},
},
]
This makes messages available in templates.
Import the messages module in your view.
from django.contrib import messages
You can then use the available message levels.
Use messages.success() to add a successful-operation message.
messages.success(
request,
"Student added successfully!"
)
The message can then be displayed in a template.
Use the messages variable in the template.
{% if messages %}
{% for message in messages %}
<p>
{{ message }}
</p>
{% endfor %}
{% endif %}
from django.contrib import messages
from django.shortcuts import redirect
def add_student(request):
# Save student
messages.success(
request,
"Student added successfully!"
)
return redirect("students")
The message can be displayed after the redirect.
Use messages.info() for general informational messages.
messages.info(
request,
"Your profile was updated."
)
Information messages can tell users about the current status of an action.
Use messages.warning() when you want to show a warning.
messages.warning(
request,
"Your subscription will expire soon."
)
Use messages.error() to display an error notification.
messages.error(
request,
"Unable to save the record."
)
Error messages can be used when an operation fails.
Django also provides a debug message level.
messages.debug(
request,
"Debug information."
)
Debug messages can be useful during development, depending on the configured message level behavior.
Django provides several standard message levels.
| Level | Purpose |
|---|---|
| DEBUG | Development/debug information. |
| INFO | General information. |
| SUCCESS | Successful operation. |
| WARNING | Warning information. |
| ERROR | Error notification. |
Django provides constants for message levels.
from django.contrib.messages import (
DEBUG,
INFO,
SUCCESS,
WARNING,
ERROR
)
These constants can be used when working with lower-level message APIs.
You can add extra CSS-related tags to a message.
messages.success(
request,
"Student saved!",
extra_tags="student-success"
)
The extra tag can be used by the template or CSS framework to style the message.
Each message has a tag that represents its level.
{% for message in messages %}
<p class="{{ message.tags }}">
{{ message }}
</p>
{% endfor %}
For example, a success message can have a success tag.
Django message tags can be combined with Bootstrap alert classes.
{% for message in messages %}
<div class="alert alert-{{ message.tags }}">
{{ message }}
</div>
{% endfor %}
This works well when your message tags correspond to Bootstrap alert classes.
A more controlled approach can map Django message levels to Bootstrap classes.
{% for message in messages %}
{% if message.tags == "success" %}
<div class="alert alert-success">
{{ message }}
</div>
{% elif message.tags == "error" %}
<div class="alert alert-danger">
{{ message }}
</div>
{% elif message.tags == "warning" %}
<div class="alert alert-warning">
{{ message }}
</div>
{% else %}
<div class="alert alert-info">
{{ message }}
</div>
{% endif %}
{% endfor %}
Messages are especially useful when redirecting after an operation.
messages.success(
request,
"Record updated successfully!"
)
return redirect("student_list")
The message can then be displayed on the redirected page.
A login view can add a message after successful authentication.
from django.contrib import messages
login(request, user)
messages.success(
request,
"Login successful!"
)
return redirect("dashboard")
A logout view can also display a message.
logout(request)
messages.success(
request,
"You have been logged out."
)
return redirect("login")
This provides immediate feedback after logout.
A registration view can show a success message after creating a user.
User.objects.create_user(
username=username,
password=password
)
messages.success(
request,
"Account created successfully!"
)
return redirect("login")
Messages can also be used when an operation fails.
if Student.objects.filter(
email=email
).exists():
messages.error(
request,
"Email already exists."
)
return redirect("add_student")
{% if messages %}
{% for message in messages %}
<div
class="alert alert-{{ message.tags }}"
>
{{ message }}
</div>
{% endfor %}
{% endif %}
Place this code in a common template when you want messages to appear throughout the application.
messages.MessageMiddleware.django.contrib.messages.Messages are useful after successful form submissions.
if form.is_valid():
form.save()
messages.success(
request,
"Form submitted successfully!"
)
return redirect("home")
This gives the user feedback without placing the message permanently in the database.
User Performs Action
↓
Django View
↓
Operation Successful?
↙ ↘
Yes No
↓ ↓
success() error()
↓ ↓
Redirect / Render
↓
Template
↓
Display Message
views.py
from django.contrib import messages
from django.shortcuts import redirect
def save_student(request):
# Save student record
messages.success(
request,
"Student saved successfully!"
)
return redirect("students")
students.html
{% if messages %}
{% for message in messages %}
<div
class="alert alert-{{ message.tags }}"
>
{{ message }}
</div>
{% endfor %}
{% endif %}
The Django messages framework provides a convenient way to display temporary notifications to users.
from django.contrib import messages.messages.success() for successful operations.messages.info() for information.messages.warning() for warnings.messages.error() for errors.messages variable in templates.message.tags for message-level styling.from django.contrib import messages.{% for message in messages %}.message.tags can be used for styling.Question: Which Django function is used to display a success message?