Django message template tag checking
Asked Answered
T

3

16

I want to check whether the message tag are INFO ?

How to check that in the template file?

I tried this but it didn't work:

{% if messages %}
    <ul>
        {% for message in messages %}
            <li>
                {% if message.tag == "INFO" %}
                    {{ message }}
                {% endif %}
            </li>
        {% endfor %}
    </ul>
{% endif %}

I even tried other combinations like message.tag.info == "INFO" etc but they didn't seem to work. What is the correct way of checking it?

Truncation answered 23/5, 2013 at 10:46 Comment(0)
H
29

Using bootstrap

{% block messages %}
    {% if messages %}
        {% for message in messages %}
            <div class="alert alert-{{ message.tags }}">  <!-- singular -->
                <a class="close" data-dismiss="alert">×</a>
                {{ message|safe }}
            </div>
        {% endfor %}
    {% endif %}
{% endblock %}

So it's {{ message.tags }} you're looking for

Hhour answered 23/5, 2013 at 10:48 Comment(3)
I understand this. But how to check if a particular tag is INFO or SUCCESS ?Truncation
{% if message.tags == 'info' %} if it only holds a single one, else you could iterate itHhour
@Andrei leveraging tags styling within django templates, its an encore. By the way, this question dates back from 2013, no idea if this is all still valid.Hhour
C
1

<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet"/>
         <!--Messages-->
 {% if messages %}
    {% for message in messages%}
    {% if message.tags == 'error' %}
    <div class="block mt-5 mx-5 text-sm text-red-600 bg-red-200 border border-red-400 `enter code here`h-12 flex items-center p-4 rounded-md relative" role="alert">
        <span class="mr-1">
            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 inline-block text-red-600" viewBox="0 0 20 20" fill="currentColor">
                <path fill-rule="evenodd" d="M10 1.944A11.954 11.954 0 012.166 5C2.056 5.649 2 6.319 2 7c0 5.225 3.34 9.67 8 11.317C14.66 16.67 18 12.225 18 7c0-.682-.057-1.35-.166-2.001A11.954 11.954 0 0110 1.944zM11 14a1 1 0 11-2 0 1 1 0 012 0zm0-7a1 1 0 10-2 0v3a1 1 0 102 0V7z" clip-rule="evenodd" />
            </svg>
          </span>
        <span>
            <strong class="mr-1">{{message}}</strong>
        </span>
          <button type="button" data-dismiss="alert" aria-label="Close" onclick="this.parentElement.remove();">
              <span class="absolute top-0 bottom-0 right-0 text-2xl px-3 py-1 hover:text-red-900" aria-hidden="true" >×</span>
          </button>
        </div>
    {% endif %}
    {% if message.tags == 'success' %}
    <div class="block mt-5 mx-5 text-sm text-teal-600 bg-teal-200 border border-teal-400 h-12 flex items-center p-4 rounded-md relative" role="alert">
          <span class="mr-1">
            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 inline-block text-teal-600" viewBox="0 0 20 20" fill="currentColor">
                <path fill-rule="evenodd" d="M2.166 4.999A11.954 11.954 0 0010 1.944 11.954 11.954 0 0017.834 5c.11.65.166 1.32.166 2.001 0 5.225-3.34 9.67-8 11.317C5.34 16.67 2 12.225 2 7c0-.682.057-1.35.166-2.001zm11.541 3.708a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd" />
            </svg>
          </span>
        <span>
            <strong class="mr-1">{{message}}</strong>
        </span>
          <button type="button" data-dismiss="alert" aria-label="Close" onclick="this.parentElement.remove();">
              <span class="absolute top-0 bottom-0 right-0 text-2xl px-3 py-1 hover:text-red-900" aria-hidden="true" >×</span>
          </button>
        </div>
    {% endif %}
    {% endfor %}
    {% endif %}
    <!-- Message End --

> 

Here you can check in html using which type of tag it is use single quote not double it will give an error.

{% if message.tags == 'error' %}
Cologne answered 2/6, 2021 at 6:11 Comment(0)
S
0

Here is a simple way I implemented this.

{% if messages %}
   {% for message in messages %}
       {% if message.tags == 'success' %}
           <div class="alert alert-success" role="alert">                      
              <em>{{ message }}</em>
           </div>
       {% endif %}
       {% if message.tags == 'warning' %}
           <div class="alert alert-warning" role="alert">                        
             <em>{{ message }}</em>
           </div>
       {% endif %}
   {% endfor %}
{% endif %}

Totally Worked for me....

Seagrave answered 24/11, 2023 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.