如何将项目填充到Jinja2模板的两列中

时间:2018-07-20 05:31:14

标签: python html flask jinja2

我有一个电话字典列表,我想将其分为两列。我有一些解决方案,但看起来很脏。正确和最简单的方法是什么?

我的解决方案在这里:

我的手机字典在这里:

    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>

它是这样的:

enter image description here

1 个答案:

答案 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 %}