Table of Content
- Set Up Messages in Django
- Levels and Tags of Messages in Django
- How to Use Messages in Django
- Extra Snippet for Message You Can Adopt in Your Django Project
- Wrap Off
Django messages provide a way of keeping the users of your application up to date with what is going on. By communicating when business logic is loading, when there's some background activity processing, and the outcome of a processed logic.
This communication makes a huge difference in the user experience, as it keeps the user informed when the application changes state.
For example, let's say a user clicks a button to save a form, and nothing happens, no alert or indication, nothing. This leaves the user wondering if the provided information in the form is saved or not. What happens next? The user most times reacts by clicking multiple times and sometimes abandoning the application for an alternative if it isn't a mandatory activity they intended to accomplish.
Here's how you can leverage the messages feature in Django to make our users more confident and comfortable when using our application, thereby improving the user experience.
Set Up Messages in Django
By default, when we create a new Django project, the messages framework is already installed.
As long as you did not alter anything in the settings regarding the messages framework, you are welcome to skip to the next section.
Otherwise, here is how you can set up messages in Django:
-
INSTALLED_APPS
- Add
django.contrib.messages
to your installed apps.
- Add
-
MIDDLEWARE or MIDDLEWARE_CLASSES
-
In older versions, add these middlewares
django.contrib.sessions.middleware.SessionMiddleware
django.contrib.messages.middleware.MessageMiddleware
-
TEMPLATES
- Add
django.contrib.messages.context_processors.messages
to the context_processors of the templates in settings.
- Add
Levels and Tags of Messages in Django
There are various levels of messages in Django for different use cases.
The table below shows the various levels of messages, their use case, and CSS classes:
Constant | Level | Tag (CSS Class) | Use case |
---|---|---|---|
DEBUG | 10 | debug | Used for development-related messages that will be ignored or removed in a production deployment |
INFO | 20 | info | Used to provide informational messages for the user |
SUCCESS | 25 | success | Used to indicate that the outcome of processing or action was successful |
WARNING | 30 | warning | Used to indicate that a failure did not occur but may be imminent |
ERROR | 40 | error | Used to indicate that the outcome of processing or action was not successful or some other failure occurred |
Django will only display messages with a level greater than 20 (INFO) by default. If you want to display DEBUG messages:
In settings.py, add the code below
If you encounter a circular import issue, you can add the constant value directly:
How to Use Messages in Django
There are two ways you can use Messages in Django.
1. Built-in Method
This method is recommended if you are using the built-in message levels.
The various built-in methods are:
- In your views views.py
- In the template:
- To add a success message to the example above, we can do something like this:
- You can also pass extra tags to the message:
messages.success(request, 'Your password was updated successfully!', extra_tags='alert')
Output:
2. Custom Method
This method is useful in defining a custom level:
Extra Snippet for Message You Can Adopt in Your Django Project
messages.html
{% for message in messages %}
<div class="alert {{ message.tags }} alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
{{ message }}
</div>
{% endfor %}
from django.contrib.messages import constants as messages
MESSAGE_TAGS = {
messages.DEBUG: 'alert-info',
messages.INFO: 'alert-info',
messages.SUCCESS: 'alert-success',
messages.WARNING: 'alert-warning',
messages.ERROR: 'alert-danger',
}
And then to use it, add messages.html to your base.html template:
base.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Simple is Better Than Complex</title>
</head>
<body>
{% include 'partials/header.html' %}
<main>
<div class="container">
{% include 'partials/messages.html' %}
{% block content %}
{% endblock %}
</div>
</main>
{% include 'partials/footer.html' %}
</body>
</html>
Wrap Off
Django messages provide a way of keeping the users of your application up to date with what is going on. By communicating the outcome of an action. It makes a huge difference in the user experience, as it keeps the user informed when the application changes state.
If you learned from this tutorial, or it helped you in any way, please consider sharing and subscribing to our newsletter.
Please share this post and for more insightful posts on business, technology, engineering, history, and marketing, subscribe to our newsletter.