【发布时间】:2018-07-22 20:01:03
【问题描述】:
我正在尝试使用 django html 模板根据结果列表创建具有动态行和列的表。记录数和标题编号可能会发生变化。我正在使用两个 for 循环来获取行数和列数。我很难尝试输出表中的实际值。这个想法是从第二个 for 循环“索引”并将其应用于第一个循环的“每个”。我知道语法肯定是错误的,但是 django 可以做到吗?如果没有,我有什么建议可以研究如何实施吗?谢谢!!
list = [
{'header_a': foo1, 'header_b': foo2, 'header_c': foo3},
{'header_a': foo1, 'header_b': foo2, 'header_c': foo3},
{'header_a': foo3, 'header_b': foo3, 'header_c': foo3},
{'header_a': foo4, 'header_b': foo4, 'header_c': foo4},
]
Sample Table
header_a | header_b | header_c
foo1 | foo1 | foo1
foo2 | foo2 | foo2
foo3 | foo3 | foo3
foo4 | foo4 | foo4
或
list_2 = [
{'header_c': c_foo1, 'header_d': d_foo1},
{'header_c': c_foo2, 'header_d': d_foo2},
]
Sample Table 2
header_c | header_d
c_foo1 | d_foo1
c_foo2 | d_foo2
<table>
<thead>
<tr>
{% for index in list.0 %}
<th>{{ index }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for each in list %}
<tr>
{% for index in a %}
<td>{{ each.{{index}} }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
【问题讨论】:
-
该列表是来自模型单个查询集还是由多个查询集组成,因此需要按顺序排列?您可以发表您的观点以便我们提供帮助吗?
-
这是从单个查询集返回的数据。
-
好的,但是,列标题如何动态更改?如果模型已经设置,除非您更改模型,否则它应该是相同的字段。
-
后端正在运行动态 sql 查询。列数可能因给定条件而异。
-
嗯,好吧,让我检查一下
标签: javascript html django django-templates frontend