将html表行拆分为另一列

时间:2016-03-14 03:58:49

标签: python html django twitter-bootstrap

我想循环我的数据来呈现表

my datas = [[1, 'count'], [2, 'count'], [3, 'count'], ..., ['total', 'count']]

继续本月的日子

我想像这样将数据渲染到表格中 所以当for循环计数器直到9时,它将创建/移动到另一列并继续渲染数据。

enter image description here

我试过这个,但横向走了:

<table class="table table-bordered table-striped">
  <tr>
    {%for item in new_client_dict %}
    <td>{{ item.0 }}</td>
    <td>{{ item.1 }}</td>
    {%if forloop.counter|divisibleby:"9"%}
  </tr>
  <tr>
    {%endif%}
    {%endfor%}
  </tr>
</table>
enter image description here 有什么建议吗?感谢

2 个答案:

答案 0 :(得分:1)

尝试使用divisible_by关闭<tr>并打开一个新行。例如:

<tr>
{%for item in items%}
<td>{{data}}</td>
{%if forloop.counter|divisible_by:"9"%}
</tr>
<tr>
{%endif%}
{%endfor%}
</tr>

答案 1 :(得分:0)

假设你不介意将它们放在分隔的表中(并排在同一行中),你使用它几天(max = 31,包含9行的4列),并且你正在使用bootstrap(如问题标签)你可以这样做:

<div class='row'>


    {%for item in items%}
        {% if forloop.counter = "1" %} #I can't tell if this counter starts with 0 or one, be sure to make it the first interation number
            <div class='col-lg-3'>
            <table class="table table-bordered table-striped">
            <tbody>
        {% endif %}
        {% if forloop.counter|divisible_by:"9" %}
            </tbody>
            </table>
            </div>
            <div class='col-lg-3'> # lg, sm, xl, choose your flavor
            <table class="table table-bordered table-striped">
             #add header here #
            <tbody>
        {% endif %}
        <tr>
            <td>{{ item.0 }}</td>
            <td>{{ item.1 }}</td>
        </tr>
     {% endfor %}
            </tbody>
            </table>
            </div>

</div>