Django Flash Messages with Message Framework

This is our seventeenth article in Django, in this article we are going to learn about Django Flash Messages with Message Framework .

so in a web application sometimes you need to display one-time notification message (also known as “flash message”) to the user after processing a form or some other types of user input.

For this, Django provides full support for cookie- and session-based messaging, for both anonymous and authenticated users. The messages framework allows you to temporarily store messages in one request and retrieve them for display in a subsequent request (usually the next one). Every message is tagged with a specific level that determines its priority (e.g., infowarning, or error).

 

 

Basically in this example we are going to create a simple registration form, when the user submit their information we want to show a flash message for them.

 

 

 

Flask Web Development Tutorials

1: Flask CRUD Application with SQLAlchemy 

2: Flask Creating News Web Application 

3: Flask Creating REST API with Marshmallow 

 

 

 

Python GUI Development Tutorials

1: PyQt5 GUI Development Tutorials

2: Pyside2 GUI Development Tutorials

3: wxPython GUI Development Tutorials

4: Kivy GUI Development Tutorials 

5: TKinter GUI Development Tutorials

 

 

 

 

So now we need to create a New Project in Django, also for django installation and creating New Project you can read this article Django Introduction & Installation. but you can use this command for creating of the New Project.

 

 

 

After creating of the Django Project, you need to migrate your project. make sure that you have changed the directory to the created project.

 

 

 

 

So after doing this, let’s create our Django App, i have already talked about Django App in one of my articles, you can check this Django Apps & URL Routing.

 

 

 

 

After creation of the App, you need to add the created App in your settings.py file INSTALLED_APP section .

 

 

 

 

Now open your models.py in your MyApp App, and add your model, this is a basic model with four fields, you can read my article on django models  Django Models Introduction.

 

 

 

 

 

After adding a new model in Django, first you need to create migrations file and after that you need to add your model to the database, by default Django uses Sqlite database, for mysql database connection you can read this article  Django Mysql Database Connection. make sure that you have changed directory to your project.

 

 

 

OK now let’s just add our model to the Django admin panel, you can read this article for django super user   Django Creating Super User. open your admin.py file and add this code.

 

 

 

For the registration we need to create a form, and also we are using ModelForm in this article, you can read my complete article on ModelForm in this link  Django Creating Form with Model Form.

it is a simple form based on our model that we have already created. the form will have four fields like username, password, email and phone.

 

 

 

 

 

Before adding our html files, we need to create our views, so open your views.py file and add this code for creating a simple view. and you can see in the view we have passed the form as context variable to the django template. the register() view is for rendering our form, and the addUser() view is for adding data from our form to the database. also you can see that after successful registration of the user we are going to create a Flash Message of success to the user. you need to import the Flash Message at the top, it is related to django.contrib package.

 

So you can see that in the addUser() view function, we are going to tell Django , that after successful registration show a flash message to the user.

 

 

 

Now we need to render this Flash Message in our html file, so you need to create a new directory in your Django Project called templates, and in the templates folder add two html files, the first one is index.html and the second one is base.html. for django templates you can read this article Django Templates Introduction. also make sure that you have added your templates name in the templates section of settings.py file like this.

 

 

 

And this is our base.html,  also you can see that in the base.html we have added the Bootstrap CDN link, because in this article we are going to use from Bootstrap styles. also you can check this article for adding styles to your Django Project Django Adding Bootstrap & CSS.

 

 

 

 

You can show the Flash Message where ever you want in your html file using this code.

 

 

 

 

After adding that code , this is our complete index.html file.

 

 

 

 

Also you need to create your urls, you can read this article for url routing Django Apps & URL Routing . but just create a new python file in your MyApp app at name of urls.py and add these codes.

 

 

 

 

And also you need to include your app urls.py in your project urls.py file.

 

 

 

 

Now you can run your project and we have our simple form. also you can add data from this form to your database. so add some data and check your admin panel, you will have the added data. also you can see a nice flash message after adding submitting the data to the database. make sure that you have changed the directory to your Django Project.

 

 

 

 

 

 

http://localhost:8000/

Django Flash Messages with Message Framework
Django Flash Messages with Message Framework

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×