Big Sale Offer - Get Any Course in $5 - (Click) - with Limited Time Offer

FULL Guide | Django messages Framework

Django messages Framework copy
Django Django Tutorials

FULL Guide | Django messages Framework

FULL Guide | Django messages Framework

In this Django tutorial, we have to discuss the Django messages framework in which we use different kinds of messages for different purposes. Actually, we need to display one-time notifications to the user while instructing the user. There may be different situations you have to use messages and show them to the user. Such kinds of messages we also say that flash message.
The one-time notification means you will get only a time notification message. For example, you sign up on any website. After completing your form for sign up you will be notified that you have been registered successfully.
This message will display one time. Another example, when you sign in or log in to your account, after login you redirect to the dashboard, you are welcome on the dashboard message will be for one time. When you visit the again dashboard with login you will not be notified at one time.
So such kinds of messages are called flash messages which can be used by the developer using the Django framework.
To use the messages framework, you have to enable it first in your settings.py file in the Middleware list. Keep in mind if you have used the following middleware then you can use messages but don’t worry, this setting is done by default when you create a Django project.
‘django.contrib.sessions.middleware.SessionMiddleware’ ‘django.contrib.messages.middleware.MessageMiddleware’.
Accorindg to Django docuemntation. We can use messages for anonymous users as well as an authenticated users also based on session and user cookie settings. To display a message, you have to need memory, but such messages store a temporary base in one request and retrieve that message for displaying to the user in a subsequent request.
Anonymous Users: Those users who are logout from their account or non-registered user.
Authenticated Users: Those users who have permission to use the limited backend area provided by the admin.

Django messages Framework Levels

There are different levels of message you can use. But it depends upon the situation and your needs. When you create a form for user registration. When you create a user registration form and the user fills in the complete providing accurate details, when a user successfully completes his/her form, then you have to use a message level ‘SUCCESS’. If the user makes any mistake then you have to use level ‘WARNING’
Type Purpose
DEBUG Development-related messages but can be ignored when the project is on a live server.
INFO Important messages for the user
SUCCESS When you performed a successful action “Your account is created successfully”
WARNING You informed a warning when you near to making a mistake
ERROR If you make any mistake and unsuccessful action then it used

How to use Django message in your project

Now, you can use any level of message in your Django project depending on your needs. So, the following example will help you in using messages.
from django.shortcuts import render, redirect
from django.contrib import messages
def std_register(request):
    if request.method == “POST”:
        form = CreateUserForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data[‘username’] # Getting username from username field
            messages.success(request, ‘Your account is created ‘+username) # displaying success message when propelry user is registerd. Then after dispalying message it will redirect to login page as mentioned below code of line.
            return redirect(‘login’)
    else:
        form = CreateUserForm()
    context = {‘form’:form}
    return render(request, ‘users/std_register.html’, context)

Conclusion

After reading this Djagn tutorial, you have understood how you can use Django Messages Framework in your project. According to my opinion, it is very important to use in your project. Because they provide a friendly environment in your application and engage your user with the application. And also such messages give instructions to users that what issue with a system or what achievement you have to succeed.

If you have any questions in your mind while reading out the tutorial, then must share them with us. We will provide you the best solution possible.

If you use Facebook, Twitter, Pinterest, LinkedIn, or other social media platforms, then share this Django tutorial, if you share, more students will take the chance to learn.

Recommended Django tutorial for Beginners

Complete Django Models | Django Tutorials

Complete Tutorials on Django ModelForm

Django Include and Extends Tags in Django Template language

Complete Guide on Django ORM | Django Tutorials

What is Django URL Tag in Django Template Language

 

Leave your thought here

Your email address will not be published. Required fields are marked *

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
  • Attributes
  • Custom attributes
  • Custom fields
Click outside to hide the comparison bar
Compare