我有一个 list :
c = [1,2,3,4,5,6,7,8]
在我的模板中,我想输出如下:
<table>
<tr>
<td>1</td>
<td>2</td>
<td>...</td>
</tr>
</table>
<table>
<tr>
<td>5</td>
<td>...</td>
<td>8</td>
</tr>
</table>
最好的方法是什么?
最佳答案
如果您想让它更通用,您还可以使用内置的 divisibleby
标签
{% for value in c %}
{% if forloop.counter0|divisibleby:cut_off %}
<table>
<tr>
{% endif %}
<td>{{value}}</td>
{% if forloop.counter|divisibleby:cut_off %}
</tr>
</table>
{% endif %}
{% endfor %}
其中 c
是列表,cut_off
是切片数(例如问题中的 4)。这些变量应该发送到您 View 中的模板。
关于python - 在一定数量的变量之后更改列表输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25658316/