This space is available for advertisement. Please contact us if interested.
Advertise with us
messages templates   0   15611
How to display flash messages in Django templates

Sometimes we need to show the one-time notification, also known as the flash messages in our Django application. For this Django provides the messages framework. We are going to use the same here.

To show flash messages in the Django application, we will extend our previous project Hello World in Django 2.2. Clone the git repository, check out the master branch and set up the project on your local machine by following the instructions in the README file.

Paste the below code snippet in the body tag of HTML file hw/templates/hw/home.html.

{% for message in messages %}
<div class="alert {{ message.tags }} alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
{{ message | safe }}
{% endfor %}

Define the messages constant. Paste the below code snippet in file.

from django.contrib.messages import constants as messages
messages.DEBUG: 'alert-info',
messages.INFO: 'alert-info',
messages.SUCCESS: 'alert-success',
messages.WARNING: 'alert-warning',
messages.ERROR: 'alert-danger',
except Exception as e:

In your view file, import the messages package.

from django.contrib import messages

Now inside the home view, set the appropriate message in request.

def home(request):
data = dict()
messages.success(request, "Success: This is the sample success Flash message.")
messages.error(request, "Error: This is the sample error Flash message."), "Info: This is the sample info Flash message.")
messages.warning(request, "Warning: This is the sample warning Flash message.")
return render(request, 'hw/home.html', data)

Refresh the home page in your browser.

flash messages django

Source Code:

Source code is available on GitHub. Checkout the branch flash_messages. 

In case of any query, feel free to comment.

cover image source:
messages templates   0   15611

Related Articles:
For loop in Django template
using for loop in Django templates, Using break in Django template for loop, Using range in django template for loop, How to access index in for loop in django template, for - empty in django template...
Django Template Fiddle Launched !!!!
Django template fiddle, Experimenting with Django templates, Playing with Django template, Django fiddle, template fiddle,,...
Solving Django Error: TemplateDoesNotExist at /app_name/
How to solve TemplateDoesNotExist error in Django projects, reason for TemplateDoesNotExist error in Django, fixing TemplateDoesNotExist error in Django...
5 lesser used Django template tags
rarely used Django template tags, lesser-known Django template tags, 5 awesome Django template tags, Fun with Django template tags,...

0 thoughts on 'How To Display Flash Messages In Django Templates'
Leave a comment:

*All Fields are mandatory. **Email Id will not be published publicly.

DigitalOcean Referral Badge

Get a .COM for just $5.98!

© 2021-2022 Python Circle   Contact   Sponsor   Archive   Sitemap   Partner Sites: PythonSnippets.Dev  99Dev.Tools