【问题标题】:Django Messages Framawork is not shown with indexDjango Messages Framawork 未显示索引
【发布时间】:2019-02-10 23:48:24
【问题描述】:

Django Messages Framawork 未显示索引。

错误信息显示正常,但是我需要在数组之前获取信息的状态。

我尝试使用消息数组的索引,但没有显示。

有人知道我哪里出错了吗?

谢谢。

{% if messages %}
    <script>
        Swal.fire({
          type: {{ messages.0.tags }},  <-------- Is not shown
          title: 'Title',
          html:   '<ul class="messages" style="list-style: none;padding: 0;">\n' +
              '        {% for message in messages %}\n'+
                  '        <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>\n'+
                  '            <div class="notification is-{% if message.tags == 'error' %}danger{% else %}success{% endif %}">\n'+
                  '                {{ message }}\n'+
                  '            </div>\n'+
                  '        </li>\n'+
                  '        {% endfor %}\n' +
              '    </ul>'
        })
    </script>
{% endif %}

【问题讨论】:

  • 检查messages.0是否打印任何东西

标签: python django python-3.x django-templates


【解决方案1】:

当您一次迭代消息时“使用”消息,您可以在一个循环中构造必要的 JS 变量,然后再使用它们:

var html = "";
{% for message in messages %}
  {% if forloop.first %}
  var type = message.tags;
  {% endif %}
  html += "<li>...html for one item</li>"
{% endfor %}
html = "<ul>" + html + "</ul>"
Swal.fire({
      type: type, 
      title: 'Title',
      html: html
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 2016-04-24
    • 2019-12-19
    • 2021-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多