我有一个电话字典列表,我想将其分为两列。我有一些解决方案,但看起来很脏。正确和最简单的方法是什么?
我的解决方案在这里:
我的手机字典在这里:
myphones = [
{'number':'13888888881',
'fee' = '1'},
{'number':'13888888882',
'fee' = '1'},
{'number':'13888888883',
'fee' = '1'},
{'number':'13888888884',
'fee' = '1'},
{'number':'13888888885',
'fee' = '1'},
{'number':'13888888886',
'fee' = '1'},
]
模板中的: 我写道:
<pre>
{% for phone in myphones %}
<tr>
<td>
{{phone.number}}
</td>
<td>
{{phone.fee}}
</td>
<td>
{{phone.number}}
</td>
<td>
{{phone.fee}}
</td>
</tr>
{% endfor %}
</pre>
它是这样的:
答案 0 :(得分:4)
尝试:
{% for i in range(0, ((myphones|length)/2)|int, 2) %}
<tr>
<td>
{{myphone[i].number}}
</td>
<td>
{{myphone[i].fee}}
</td>
{% if (i+1) <= (myphone|length-1)%}
<td>
{{myphone[i+1].number}}
</td>
<td>
{{myphone[i+1].fee}}
</td>
{% endif %}
</tr>
{% endfor %}