【问题标题】:Django message template tag checkingDjango消息模板标签检查
【发布时间】:2013-05-18 16:39:45
【问题描述】:

我想检查message tag是否是INFO?

如何在模板文件中检查?

我试过了,但没用:

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

我什至尝试过其他组合,例如 message.tag.info == "INFO" 等,但它们似乎不起作用。检查的正确方法是什么?

【问题讨论】:

    标签: django django-templates django-messages


    【解决方案1】:

    使用引导

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

    所以你要找的是{{ message.tags }}

    【讨论】:

    • 我明白这一点。但是如何检查特定标签是INFO 还是SUCCESS
    • {% if message.tags == 'info' %} 如果它只有一个,否则你可以迭代它
    【解决方案2】:

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

    在这里你可以检查 html 使用哪种类型的标签,使用单引号而不是双引号会出错。

    {% if message.tags == 'error' %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-05
      • 1970-01-01
      • 2011-08-25
      • 2021-02-18
      • 1970-01-01
      • 2011-02-09
      • 2012-10-31
      相关资源
      最近更新 更多