【问题标题】:Looping in django template creating earror in table data在 django 模板中循环在表数据中创建错误
【发布时间】:2018-05-10 19:37:24
【问题描述】:

我在 django 模板中使用了一个表格。我将上下文中的两个对象数组传递给模板。当我遍历两个对象数组时,如果条件失败,就会产生问题。如果条件不满足,它会将后面的 td 的值放在前面的值中。然后,如果条件不满足,我尝试应用 else 来提供空白 td。但是由于 else 条件,它会在所有条件下创建跳过前四个块。

如果没有满足 if 条件,我只想在 td(table data) 中先给空格。

 <table id="searchFilterNewAdmin" class="table table-striped table-bordered table-hover" >
    <thead>
        <tr role="row">
            <th class="sorting_asc" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-sort="ascending" aria-label="Rendering engine: activate to sort column descending">Admin Name</th>
            <th class="sorting" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-label="Browser: activate to sort column ascending">Tablet No</th>
            <th class="sorting" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-label="Platform(s): activate to sort column ascending">Region</th>
            <th class="sorting" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-label="Engine version: activate to sort column ascending">Car Make</th>
            <th class="sorting" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-label="CSS grade: activate to sort column ascending">Total Time(s)</th>
            <th class="sorting" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-label="CSS grade: activate to sort column ascending">Last Opened</th>
            <th class="sorting" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-label="CSS grade: activate to sort column ascending">No.of Clicks</th>
            <th class="sorting" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-label="CSS grade: activate to sort column ascending">Average Time(s)</th>
        </tr>
    </thead>
    <tbody>
        {% for all in admin_time_stats %}
            <tr>
                {% for every in all_tabletadmins %}
                    {% if all.tablet_number == every.tablet_number %}
                        <td>{{ every.first_name }}</td>
                        <td>{{ every.tablet_number }}</td>
                        <td>{{ every.region }}</td>
                        <td>{{ every.car_year}}</td>
                    {% else %}
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                    {% endif %}
                {% endfor %}
                <td>{{ all.total_time }}</td>
                <td>{{ all.start_time }}</td>
                <td>{{ all.no_of_clicks }}</td>
                <td>{% widthratio all.total_time all.no_of_clicks 1 %}</td>
            </tr>
        {% endfor %}
    </tbody>
</table>

admin_time_stats = [{'no_of_clicks': 15, 'tablet_number': 'tablet9', 'start_time': datetime.datetime(2017, 11, 24, 13, 57, 8, 64000, tzinfo=), 'total_time': 480}{'no_of_clicks': 1, 'tablet_number': 'tablet10', 'start_time': datetime.datetime(2017, 11, 8, 17, 18, 27, 389000, tzinfo=), 'total_time': 32}]


all_tabletadmins = [{'tablet_number': 'tablet9', 'region': 'usa', 'car_year': '1991', 'admin_name': 'sam' }]

【问题讨论】:

  • 您能否发布一个示例,说明您的两个列表admin_time_statsall_tabletadmins 的样子?
  • 当然,我编辑了问题并添加了它
  • 您可以尝试只删除 else 块中的 &lt;td&gt;s 以便 else 基本上是无操作的吗?

标签: python html django templates jinja2


【解决方案1】:

你在&lt;/tr&gt;标签后面添加了{% endfor %}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 2023-01-19
    • 2017-01-05
    • 1970-01-01
    相关资源
    最近更新 更多