问题描述
我想在Django中做一些非常简单的事情:将矩阵打印成HTML表,并为行和列添加标签。这些是我的观点中的变量: matrix = np.array([
[101,102,103],
[201,202,203],
])
colnames = ['Foo','Bar','Barf']
rownames = ['Spam','Eggs']
我想要一张看起来像这样的表:
Foo Bar Barf
垃圾邮件101 102 103
鸡蛋201 202 203
我的模板代码如下所示:
<表>
< tr>
< th>< / th>
{%colinames中的colname%}
< th> {{colname}}< / th>
{%endfor%}
< / tr>
{%rownames中的rowname%} {%with forloop.counter0 as rowindex%}
< tr>
< th> {{rowname}}< / th>
{%colnames中的colname%} {%with forloop.counter0 as colindex%}
< td> TABLECELL< / td>
{%endwith%} {%endfor%}
< / tr>
{%endwith%} {%endfor%}
< / table>
输出不同值的TABLECELL:
{{rowindex}},{{colindex}}
- >表格,索引为 :)
{{matrix.0.0}}
- >充满101s :)
{{matrix.rowindex.colindex}}
- >具有空单元格的表格 :(
由于前两项工作,假设最后一个会给出预期的结果似乎并不奇怪,我唯一的解释是 rowindex
和 colindex
可能是字符串 - 当然 int()
在Django模板中禁止的许多事情之中。
有人知道我能做什么吗?或者理想情况下:
有没有人知道这是如何要在Django中完成?
编辑1:
似乎我必须将枚举列表传递给我提供它们作为 enum_colnames
和 enum_rownames
,但现在我甚至不能执行嵌套for循环: / p>
< table>
< tr>
< th& < / th>
{%for unused_colindex,enum_colnames%中的colname}
< th> {{colname}}< / th>
{%endfor%}
< / TR>
{%for rowindex,rowname in enum_rownames%}
< tr>
< th> {{rowname}}< / th>
{%for ennt_work_anyway in enum_colnames%}
< td>你看不到我。< / td>
{%endfor%}
< / tr>
{%endfor%}
< / table>
这个表格包含所有的< th>
s填写正确的标签,但根本没有$ code>< td> 。
编辑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%}<! - 如果顶行:th - >
{%循环'< tr>< th>''< th>''< th>''< th>'%}
{%else%}< - 其他:td - >
{%循环'< tr>< th>''< td>''< td>''< td>'%}
{%endif%}
{{d}}
{%if i< 4%}<! - 如果顶行:th - >
{%cycle'< / th>''< / th>''< / th>''< / th>< / tr>'%}
{%else% }<! - else:td - >
{%cycle'< / th>''< / th>''< / td>''< / td>< / tr>'%}
{%endif% }
{%endfor%}
< / table>
注意它只能用于特定宽度的表。所以在这种形式下,它甚至不是最初的问题的真正解决方案。
感谢Paulo Almeida提供两个必要条件在他的答案中提示:
-
表格 - 包括标签 - 应该在视图中构建。
-
forloop.first
可用于将标签放在< th>
s。
table = [
['','Foo','Bar','Barf'],
['垃圾邮件',101,102,103],
['Eggs',201, 203],
]
<表>
{%表%}中的行
< tr>
{%for row in row%}
{%if forloop.first或forloop.parentloop.first%}< th> {%else%}< td> {%endif%}
{{cell}}
{%if forloop.first或forloop.parentloop.first%}< / th> {%else%}< / td> {%endif%}
{%endfor%}
< / tr>
{%endfor%}
< / table>
我想我的隐含问题的答案 - 如何从模板访问多维数组的值 - 这是不可能的。
I want to do something very simple in Django: Print a matrix as an HTML table, and add labels for the rows and columns. These are the variables in my view:
matrix = np.array([
[101, 102, 103],
[201, 202, 203],
])
colnames = ['Foo', 'Bar', 'Barf']
rownames = ['Spam', 'Eggs']
I want a to get a table that looks like this:
Foo Bar Barf Spam 101 102 103 Eggs 201 202 203
My template code looks like this:
<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>
Output for different values of TABLECELL:
{{ rowindex }}, {{ colindex }}
--> table with the indices :)
{{ matrix.0.0 }}
--> table full of 101s :)
{{ matrix.rowindex.colindex }}
--> table with empty cells :(
As the first two things work, it doesn't seem crazy to assume that the last one would give the intended result. My only explanation is that rowindex
and colindex
might be strings — and of course int()
is among the many things that are forbidden in Django templates.
Does anyone know how I can make this work? Or ideally:Does anyone know how this is intended to be done in Django?
EDIT 1:
It seems I have to pass the enumerated lists to the template. I provide them as enum_colnames
and enum_rownames
, but now I can't even execute the nested for loop:
<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>
This gives a table with all the <th>
s filled with the correct labels, but no <td>
s at all.
EDIT 2:
I found an insanely ugly "solution", which I publish here as an example of something that "works", but is clearly not an answer to my question — how this should be done in Django. Here it comes:
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>
Note how it can only be used for tables of this particular width. So in this form it is not even a real solution for the initial problem.
Thanks to Paulo Almeida for providing the two essential hints in his answer:
The table — including labels — should be built in the view.
forloop.first
can be used to put the labels in<th>
s.
table = [
['', 'Foo', 'Bar', 'Barf'],
['Spam', 101, 102, 103],
['Eggs', 201, 202, 203],
]
<table>
{% for row in table %}
<tr>
{% for cell in row %}
{% if forloop.first or forloop.parentloop.first %} <th> {% else %} <td> {% endif %}
{{ cell }}
{% if forloop.first or forloop.parentloop.first %} </th> {% else %} </td> {% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>
I guess the answer to my implicit question — how the values of multidimensional arrays can be accessed from the template — is that this is not possible.
这篇关于在Django模板中打印带有标签的矩阵的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!