Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
messages templates   0   23322
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>
</button>
{{ message | safe }}
</div>
{% endfor %}


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

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


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.")
messages.info(request, "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: https://pixabay.com/photos/the-flash-flash-marvel-superhero-2977924/
messages templates   0   23322
0 comments on 'How To Display Flash Messages In Django Templates'
Login to comment


Related Articles:
Using IF ELSE condition in Django template
how to use IF ELSE in Django template, Syntax of IF, ELIF and ELSE in Django, Using filters within IF condition in Django template, Multiple elif (else if) confitions in Django template...
How to set a variable in Django template
Declaring a new variable in Django template, Set the value of a variable in Django template, using custom template tag in Django, Defining variables in Django template tag...
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,...
Django Template Fiddle Launched !!!!
Django template fiddle, Experimenting with Django templates, Playing with Django template, Django fiddle, template fiddle, djangotemplatefiddle.com,...
DigitalOcean Referral Badge

© 2024-2025 Python Circle   Contact   Sponsor   Archive   Sitemap