Wednesday, October 23, 2019

Using the Django messages framework

Django has a built-in messages framework that allows you to display one-time notifications to your users.


The messages framework is located at django.contrib.messages and is included in the default INSTALLED_APPS list of the settings.py file when you create new projects using python manage.py startproject. You will note that your settings file contains a middleware named django.contrib.messages.middleware.MessageMiddleware in the MIDDLEWARE settings.


The messages framework provides a simple way to add messages to users. Messages are stored in a cookie by default (falling back to session storage), and they are displayed in the next request the user
does. You can use the messages framework in your views by importing the messages module and adding new messages with simple shortcuts, as follows:

from django.contrib import messages
messages.error(request, 'Something went wrong')

You can create new messages using the add_message() method or any of the following shortcut methods:

success(): Success messages to be displayed after an action was successful
info(): Informational messages
warning(): Something has not yet failed but may fail imminently
error(): An action was not successful, or something failed
debug(): Debug messages that will be removed or ignored in a production environment

Let's add messages to our platform. Since the messages framework applies globally to the project, we can display messages for the user in our base template. Open the base.html template of the account
application and add the following code between the <div> element with the header ID and the <div> element with the content ID:

{% if messages %}
<ul class="messages">
{% for message in messages %}
<li class="{{ message.tags }}">
{{ message|safe }}
<a href="#" class="close">x</a>
</li>
{% endfor %}
</ul>
{% endif %}

The messages framework includes the context  processor django.contrib.messages.context_processors.messages that adds a messages variable to the request context. You can find it in the context_processors list of the TEMPLATES setting of your project. You can use this variable in your templates to display all existing messages to the user.

Now, let's modify our edit view to use the messages framework. Edit the views.py file of the account application, import messages, and make the edit view look as follows:

from django.contrib import messages
@login_requireddef edit(request):
if request.method == 'POST':
# ...
if user_form.is_valid() and profile_form.is_valid():
user_form.save()
profile_form.save()
messages.success(request, 'Profile updated '\
'successfully')
else:
messages.error(request, 'Error updating your profile')
else:
user_form = UserEditForm(instance=request.user)
# ...

We add a success message when the user successfully updates their profile. If any of the forms contain invalid data, we add an error message instead.

Open http://127.0.0.1:8000/account/edit/ in your browser and edit your profile. When the profile is successfully updated, you should see the following message:



When data is not valid, for example, using an incorrectly formatted date for the Date of birth field, you should see the following message:





Here I am ending today's post. You can learn more about the messages framework at https://docs.djan
goproject.com/en/2.0/ref/contrib/messages/.




Share:

0 comments:

Post a Comment