Reputation: 45
I am trying to display a message on order creation success or failure. For messages.success(request, "My message")
it is working as expected. But for messages.error(request, "My message")
it is not as expected. I read the django messages framework docs, but no use. Can someone tell me why is this happening
Failed Message: This is supposed to be red alert if I am not wrong. Here's my html file.
base.html
<main role="main" class="container" >
{% if messages %}
{% for message in messages %}
<div class="alert alert-{{ message.tags }}">
{{ message }}
</div>
{% endfor %}
{% endif %}
views.py
if verify:
if response_dict['RESPCODE'] == '01':
messages.success(
request, "Thank you for ordering! Your items will be delivered soon")
return redirect(reverse('update-records', kwargs={'order_id': order_id}))
else:
messages.error(
request, "Your order could not be placed, here are the details: " + response_dict['RESPMSG'])
return redirect(reverse('profile-page'))
Upvotes: 2
Views: 8260
Reputation: 21
Maybe try to do in views.py instead of doing:
messages.error(request, "Your order could not be placed, here are the details: " + response_dict['RESPMSG'])
do it:
messages.warning(request, "Your order could not be placed, here are the details: " + response_dict['RESPMSG'])
Upvotes: 0
Reputation: 511
This is because alert-error
is not a bootstrap class.
The according class is named alert-danger
.
Generally the tags line up nicely between bootstrap and django, this is why your code works. But as you see "error" != "danger".
To fix the issue replace
<div class="alert alert-{{ message.tags }}">
{{ message }}
</div>
with
{% if message.tags == "error" %}
<div class="alert alert-danger">
{{ message }}
</div>
{% else %}
<div class="alert alert-{{ message.tags }}">
{{ message }}
</div>
{% endif %}
Upvotes: 0
Reputation: 180
just use messages.warning
instead, it would at least show a color.
Upvotes: 0
Reputation: 1
In your HTML:
<script>
setTimeout(function () {
$('#flash').fadeOut('fast');
},5000);
</script>
<div id="flash">
{% if messages %}
{% for message in messages %}
<div class="alert alert-{{ message.tags}} m-0" role="alert">
<strong>{{ message }}</strong>
</div>
{% endfor %}
{% endif %}
</div>
in django settings.py:
from django.contrib.messages import constants as messages
MESSAGE_TAGS = {
messages.ERROR: 'danger'
}
in django views:
from django.contrib import messages
if verify:
if response_dict['RESPCODE'] == '01':
messages.success(
request, "Thank you for ordering! Your items will be delivered soon")
return redirect(reverse('update-records', kwargs={'order_id': order_id}))
else:
messages.error(
request, "Your order could not be placed, here are the details: " + response_dict['RESPMSG'])
return redirect(reverse('profile-page'))
Upvotes: 0
Reputation: 1168
If the problem is alert-error
not working you can use this after have the import statement of your message :
from django.contrib.messages import constants as messages
MESSAGE_TAGS = {
messages.ERROR: 'danger'
}
Reference ->
https://docs.djangoproject.com/en/3.0/ref/contrib/messages/#message-tags
Upvotes: 3
Reputation: 45
@Sowjanya R Bhat
The method you suggest can be hardcoded but I need to know, why isn't it implementing red alert my default. Your suggestion works however
<main role="main" class="container" >
{% if messages %}
{% for message in messages %}
{% if message.tags == "error"%}
<div class="alert alert-danger">
{{ message }}
</div>
{% else %}
<div class="alert alert-success">
{{ message }}
</div>
{% endif %}
{% endfor %}
{% endif %}
Upvotes: 0