简体   繁体   中英

Error message not working as expected in Django

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

Success Message: 成功讯息

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'))

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

@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 %}
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'))

just use messages.warning instead, it would at least show a color.

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 %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM