在 Django 模板中打印带有标签的矩阵的正确方法是什么? [英] What's the correct way to print a matrix with labels in a Django template?
问题描述
我想在 Django 中做一些非常简单的事情:将矩阵打印为 HTML 表格,并为行和列添加标签.这些是我认为的变量:
matrix = np.array([[101, 102, 103],[201, 202, 203],])colnames = ['Foo', 'Bar', 'Barf']rownames = ['垃圾邮件','鸡蛋']
我想要一个看起来像这样的表:
<前>Foo Bar Barf垃圾邮件 101 102 103鸡蛋 201 202 203我的模板代码如下所示:
<tr><th></th>{% for colnames in colnames %}<th>{{ colname }}</th>{% 结束为 %}</tr>{% for rowname in rownames %}{% with forloop.counter0 as rowindex %}<tr><th>{{rowname }}</th>{% for colname in colnames %}{% with forloop.counter0 as colindex %}<td>平板电池</td>{% endwith %}{% endfor %}</tr>{% endwith %}{% endfor %}
TABLECELL 不同值的输出:
{{ rowindex }}, {{ colindex }}
--> 带有索引的表 :)
{{ matrix.0.0 }}
--> 满是 101 的表格 :)
{{ matrix.rowindex.colindex }}
--> 带有空单元格的表格 :(
由于前两件事起作用,因此假设最后一件会给出预期结果似乎并不疯狂.我唯一的解释是 rowindex
和 colindex
可能是字符串——当然 int()
是 Django 模板中禁止的许多东西之一.
有谁知道我怎样才能做到这一点?或者理想情况下:有谁知道这是如何在 Django 中完成的?
编辑 1:
看来我必须将枚举列表传递给模板.我将它们提供为 enum_colnames
和 enum_rownames
,但现在我什至无法执行嵌套的 for 循环:
<tr><th></th>{% 表示未使用的colindex,enum_colnames 中的colname %}<th>{{ colname }}</th>{% 结束为 %}</tr>{% for rowindex, rowname in enum_rownames %}<tr><th>{{rowname }}</th>{% for dont_work_anyway in enum_colnames %}<td>你看不见我.</td>{% 结束为 %}</tr>{% 结束为 %}
这给出了一个表格,其中所有 编辑 2: 我发现了一个非常丑陋的解决方案",我在这里发布它作为有效"的例子,但显然不是我问题的答案——这应该如何在 Django 中完成.来了: 请注意它如何仅用于此特定宽度的表格.所以在这种形式下,它甚至不是最初问题的真正解决方案. 感谢 Paulo Almeida 在他的回答中提供了两个基本提示: 表格——包括标签——应该建立在视图中. 我猜我隐含的问题——如何从模板访问多维数组的值——的答案是这是不可能的. 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: I want a to get a table that looks like this: My template code looks like this: Output for different values of TABLECELL: 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 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 This gives a table with all the 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:
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.
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 模板中打印带有标签的矩阵的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! 都填充了正确的标签,但根本没有 . derp = ['', 'Foo', 'Bar', 'Barf', 'Spam', 101, 102, 103, 'Eggs', 201,202, 203]iderp = 枚举(derp)
{% for i, d in iderp %}{% 如果 i <4 %} {%循环'
'' '' '' '%}{% else %} {%循环' ''<td>''<td>''<td>'%}{% 万一 %}{{ d }}{% 如果 i <4 %} {% 周期</th>"'</th>''</th>''</th></tr>'%}{% else %} {% 周期</th>"'</th>''</td>''</td></tr>'%}{% 万一 %}{% 结束为 %} forloop.first
可用于将标签放在 s 中. table = [['', 'Foo', 'Bar', 'Barf'],['垃圾邮件', 101, 102, 103],['鸡蛋', 201, 202, 203],]
{% 为表中的行 %}<tr>{% 用于行 %} 中的单元格{% if forloop.first 或 forloop.parentloop.first %}
;{% else %} {% 万一 %}{{ 细胞 }}{% if forloop.first 或 forloop.parentloop.first %} </th>{% else %} </td>{% 万一 %}{% 结束为 %}</tr>{% 结束为 %} matrix = np.array([
[101, 102, 103],
[201, 202, 203],
])
colnames = ['Foo', 'Bar', 'Barf']
rownames = ['Spam', 'Eggs']
Foo Bar Barf
Spam 101 102 103
Eggs 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>
{{ rowindex }}, {{ colindex }}
--> table with the indices :){{ matrix.0.0 }}
--> table full of 101s :){{ matrix.rowindex.colindex }}
--> table with empty cells :(rowindex
and colindex
might be strings — and of course int()
is among the many things that are forbidden in Django templates.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>
<th>
s filled with the correct labels, but no <td>
s at all.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>
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>
登录
关闭