代码之家  ›  专栏  ›  技术社区  ›  marcelo.delta

Django消息framawork不与索引一起显示

  •  0
  • marcelo.delta  · 技术社区  · 7 年前

    Django消息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 %}
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Bernhard Vallant    7 年前

    当消息在迭代一次时被“使用”,您可以在一个循环中构造必要的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
    })