How to show success message in django
How to display flash message in django ?
In django, the messages framework allows you to temporarily store messages and it is automatically destroyed after it is displayed once.
There are 5 level of displaying messages
- messages.debug(request, '%s SQL statements were executed.' % count)
- messages.info(request, 'Three credits remain in your account.')
- messages.success(request, 'Profile details updated.')
- messages.warning(request, 'Your account expires in three days.')
- messages.error(request, 'Document deleted.')
For more details info, you can visit the documentation here
Now let us see how to use the messages with our example :
Open the views.py file where we had written the code to insert data into database and add the message code as shown below:
To display the message in the add product page, just iterate the messages using a for loop. The messages is like a temporary global storage so you don't have to pass it with the content in the render(). You can print the message as shown below:
This will display the message in a simple text format. If you are using bootstrap, you can use the alert class for displaying the messages. Below given is a sample code for the bootstrap alert.
Latest PostsREST API using Django Rest Framework How to connect Django project with MySQL database How to delete image from folder when deleting a record in django How to update new image in django How to fetch / retrieve images in django How to upload image in django Deploy Django App on Heroku for free Upload Files/Images in Django How to show success message in django Delete data from database in Django
© Copyright - 2018 - 2022 | All rights reserved at Funda of Web IT