[英]Rendering a list as a 2-column html table in a django template
我有一個有序列表,我在Django模板中呈現為2列html表。 在表中,應輸出元素,以便第一個(n + 1)/ 2個元素位於第一列,其余元素位於第二列,如下所示:
<table>
<tr>
<td>Elem 1</td>
<td>Elem 4</td>
</tr>
<tr>
<td>Elem 2</td>
<td>Elem 5</td>
</tr>
<tr>
<td>Elem 3</td>
<td></td>
</tr>
</table>
假設我的元素在上下文中為{{ elems }}
,我怎么能在Django模板中合理地完成這個?
檢查這個食譜 。 您可能需要修改它,但您會明白這一點。
使用它,在您的模板中,您只需:
{% for row in elems|columns:2 %}
<tr>
{% for item in row %}
<td>{{ item }}</td>
{% endfor %}
</tr>
{% endfor %}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.