1000 Python Questions
Get 1 Python question daily. Join this telegram channel https://t.me/python1000questions
Advertise with us
messages templates   0   5654
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   5654

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, djangotemplatefiddle.com,...
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.

SUBSCRIBE
Please subscribe to get the latest articles in your mailbox.

© 2017-2020 Python Circle   Contact   Sponsor   Archive   Sitemap