2

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: Success Message

Failed 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'))
Sanjay
  • 33
  • 1
  • 7
  • try printing `{{ message.tags }}` in your `div` . whether it is printing the right css class or not – Sowjanya R Bhat May 22 '20 at 06:46
  • It will just display the tag name depending on failure/successful i.e., `'success' for message.success & 'error' for message.error` – Sanjay May 22 '20 at 06:51
  • 1
    so `alert-error` class is applying ? if you try to make a static div like so : `
    red alert worked
    ` does it show you the alert in red ?
    – Sowjanya R Bhat May 22 '20 at 06:59

5 Answers5

3

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
  • 1,128
  • 10
  • 19
  • It didn't work, I am getting the error message as before `from django.contrib import messages MESSAGE_TAGS = { messages.ERROR: 'danger' }` – Sanjay May 22 '20 at 07:34
  • The above method is not overriding error-->danger. Checked printing `message.tags`...it still shows message tag as _error_ meaning it's still `alert-error` – Sanjay May 22 '20 at 07:45
  • Sorry the import is different , i have updated in answer also - `from django.contrib.messages import constants as messages ` – Sowjanya R Bhat May 22 '20 at 07:59
  • Even this didn't work. I tried this even before you suggested, checking the doc you shared, So went back to old way of hardcoding. **module 'django.contrib.messages.constants' has no attribute 'error'** we would get this error if we try importing `from django.contrib.messages import constants as messages` – Sanjay May 23 '20 at 09:51
0

@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 %}
Sanjay
  • 33
  • 1
  • 7
  • you can see what you are showing here is using `alert-danger` class . but what is coming fro your backend is `alert-error` class . i was asking you if `alert-error` works when you hard-code ? – Sowjanya R Bhat May 22 '20 at 07:20
  • No this `alert-error` doesn't work . Only `alert-danger` works, not sure why..!!! – Sanjay May 22 '20 at 07:25
  • Because bootstrap has built-in `alert-danger` class not `alert-error` – Hisham___Pak May 22 '20 at 08:20
0
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'))
0

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

0

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 %}
Samuel
  • 388
  • 5
  • 15