【问题标题】:The "Else if" statement in my Django templates appears after the HTML header block我的 Django 模板中的“Else if”语句出现在 HTML 标头块之后
【发布时间】:2021-05-23 09:17:27
【问题描述】:

我有一些数据从我的视图传递到我的模板,只要 else if 语句为真(即 if 语句为假),该语句出现在我的 HTML header() 之后。

我的主要问题是为什么 else 块不在我将其放入代码的 HTML 标头下

模板.html

<table class="table table-borderless table-data3">
    <thead>
        <tr>
            <th>No</th>
            <th>Email</th>
            <th>Telephone</th>
            <th>Country</th>
            <th>Status</th>
            <th>Image</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        {% if supplier %}
        {% for supplier in suppliers %}
        <tr>
            <td>{{forloop.counter}}</td>
            <td>{{supplier.email}}</td>
            <td>{{supplier.telephone}}</td>
            <td>{{supplier.country}}</td>
            <td class="process">Active</td>
            <td>{{supplier.image.url}}</td>
            <td>
                <div class="table-data-feature">
                    <button class="item" data-toggle="tooltip" data-placement="top" title="Edit">
                        <i class="zmdi zmdi-edit"></i>
                    </button>
                    <button class="item" data-toggle="tooltip" data-placement="top" title="Delete">
                        <i class="zmdi zmdi-delete"></i>
                    </button>
                </div>
            </td>
        </tr>
        {% endfor %}
        {% else %}
            <h2>No supplier available</h2>
        {% endif %}
    </tbody>
</table>

【问题讨论】:

    标签: python html django django-views django-templates


    【解决方案1】:

    你不能在&lt;tbody&gt; 中写h2,或者至少不能直接写。 Yolu 写这个包裹在&lt;tr&gt;&lt;td&gt;

    {% if suppliers %}
        …
    {% else %}
        <tr><td colspan="7">No supplier available</td></tr>
    {% endif %}

    您还在if 声明中打错字:它是suppliers,而不是supplier


    注意:Django 有一个{% for … %}…{% empty %} template tag [Django-doc] 如果您迭代的集合为空,则可用于呈现消息。

    【讨论】:

      【解决方案2】:

      您可以使用 {% for ... %}...{% empty %} django 模板标签。示例:

      HTML

              {% for supplier in suppliers %}
              <tr>
                  <td>{{forloop.counter}}</td>
                  <td>{{supplier.email}}</td>
                  <td>{{supplier.telephone}}</td>
                  <td>{{supplier.country}}</td>
                  <td class="process">Active</td>
                  <td>{{supplier.image.url}}</td>
                  <td>
                      <div class="table-data-feature">
                          <button class="item" data-toggle="tooltip" data-placement="top" title="Edit">
                              <i class="zmdi zmdi-edit"></i>
                          </button>
                          <button class="item" data-toggle="tooltip" data-placement="top" title="Delete">
                              <i class="zmdi zmdi-delete"></i>
                          </button>
                      </div>
                  </td>
              </tr>
              {% empty %}
                   <div><h2>No Suppliers Available</h2></div>
              {% endfor %}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-23
        • 1970-01-01
        • 2013-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-15
        • 1970-01-01
        相关资源
        最近更新 更多