It is important to have your application notify the users about every major actions that they have taken. Actions like saving, updating deleting etc. In this section of the tutorial, i will show you how we can add that function in our invoice management system application.
1. Djang has a builtin messaging module that we will use to make this possible.
In views.py, import messages module
from django.contrib import messages
2. There are different function in the message module. We will make use of the success function here to let the users know their actions were successful.
Paste the following code immediately below form.save()
messages.success(request, 'Successfully Saved')
3. At this section, we need to have a placeholder where the message will be displayed. This can be anywhere within the application. In this example, I will display the message/notification at the top of the page.
Paste the code in the template where you want to display the message
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
4. Now we are ready to test the notification. But just before we test, lets style the message to make it look more appealing to the system users.
.success{
list-style: none;
background-color: #2e6da4;
color: white;
box-shadow: 12px 12px 12px #e61c66;
text-align: center;
}
That’s all you need to do the get your system to display messages or notifications on the browser.
If you find value in my work, you might consider becoming our Patreon. Click here or follow this link: https://www.patreon.com/arbadjie to join.
You will have the full source code of all the application we have teached on this website and youtube.
Don’t forget to subscribe on my youtube www.youtube.com/c/arbadjie