【发布时间】:2015-04-12 00:37:35
【问题描述】:
我想在 Django 中做一些非常简单的事情:将矩阵打印为 HTML 表格,并为行和列添加标签。这些是我认为的变量:
matrix = np.array([
[101, 102, 103],
[201, 202, 203],
])
colnames = ['Foo', 'Bar', 'Barf']
rownames = ['Spam', 'Eggs']
我想要一个如下所示的表格:
富吧酒吧 垃圾邮件 101 102 103 鸡蛋 201 202 203我的模板代码如下所示:
<table>
<tr>
<th></th>
{% for colname in colnames %}
<th>{{ colname }}</th>
{% endfor %}
</tr>
{% for rowname in rownames %}{% with forloop.counter0 as rowindex %}
<tr>
<th>{{ rowname }}</th>
{% for colname in colnames %}{% with forloop.counter0 as colindex %}
<td>TABLECELL</td>
{% endwith %}{% endfor %}
</tr>
{% endwith %}{% endfor %}
</table>
TABLECELL 不同值的输出:
{{ rowindex }}, {{ colindex }} --> 带有索引的表 :)
{{ matrix.0.0 }} --> 满桌 101 :)
{{ matrix.rowindex.colindex }} --> 带有空单元格的表格 :(
由于前两件事情有效,假设最后一件事情会产生预期的结果似乎并不疯狂。我唯一的解释是 rowindex 和 colindex 可能是字符串——当然 int() 是 Django 模板中被禁止的许多内容之一。
有谁知道我怎样才能做到这一点?或者理想情况下: 有谁知道这是如何在 Django 中完成的?
编辑 1:
看来我必须将枚举列表传递给模板。我将它们提供为 enum_colnames 和 enum_rownames,但现在我什至无法执行嵌套的 for 循环:
<table>
<tr>
<th></th>
{% for unused_colindex, colname in enum_colnames %}
<th>{{ colname }}</th>
{% endfor %}
</tr>
{% for rowindex, rowname in enum_rownames %}
<tr>
<th>{{ rowname }}</th>
{% for doesnt_work_anyway in enum_colnames %}
<td>You don't see me.</td>
{% endfor %}
</tr>
{% endfor %}
</table>
这给出了一个表格,其中所有<th>s 都填充了正确的标签,但根本没有<td>s。
编辑 2:
我发现了一个非常丑陋的“解决方案”,我在这里发布它作为“有效”的示例,但显然不是我的问题的答案——这应该在 Django 中如何完成.来了:
derp = ['', 'Foo', 'Bar', 'Barf', 'Spam', 101, 102, 103, 'Eggs', 201, 202, 203]
iderp = enumerate(derp)
<table>
{% for i, d in iderp %}
{% if i < 4 %} <!-- if top row: th -->
{% cycle '<tr><th>' '<th>' '<th>' '<th>' %}
{% else %} <!-- else: td -->
{% cycle '<tr><th>' '<td>' '<td>' '<td>' %}
{% endif %}
{{ d }}
{% if i < 4 %} <!-- if top row: th -->
{% cycle '</th>' '</th>' '</th>' '</th></tr>' %}
{% else %} <!-- else: td -->
{% cycle '</th>' '</th>' '</td>' '</td></tr>' %}
{% endif %}
{% endfor %}
</table>
注意它只能用于这种特定宽度的表格。所以在这种形式下,它甚至不是初始问题的真正解决方案。
【问题讨论】:
-
关于编辑 2,如果您使用平面列表,您还需要将列数传递给模板。这就是为什么我建议使用与矩阵结构相同的表格。我现在添加了关于如何构建它的建议。如果使用模板的
forloop.first,可以控制第一列是否为<th>。 -
让我们暂时忘记
<th>与<td>的麻烦,假设我使用平面列表并将列数传递为width。然后我需要像{% cycle '<tr><th>' (width-1)*'<td>' %}这样的东西。存在吗? -
否,但您可以使用
forloop.counter来做您需要的事情。我没有多想,因为我同意你的观点,这是一个非常丑陋的解决方案。您对我在模板外构建表格的回答有疑问吗? -
在视图中构建表格从来都不是我的问题,但使用 Django 模板语言格式化 HTML 过去和现在都是。
forloop.first是获得<th>与<td>正确的一个很好的提示。我想我现在有一个实际的解决方案。
标签: django django-templates django-views nested-loops