How to for-loop three columns per row in Django/python?

前端 未结 2 833
不知归路
不知归路 2020-12-31 18:32

I would like to display data, three columns per row during my for. I would like my result to look like the following:

VALUE
相关标签:
2条回答
  • 2020-12-31 19:04

    There's a divisibleby tag.

    So you can do something (ugly) like:

    <table><tr>
    {% for field in form %}
       <td>{{ field }}</td>
       {% if forloop.last %}
         </tr>
       {% else %}
         {% if forloop.counter|divisibleby:"3" %}
           </tr><tr>
         {% endif %}
       {% endif %}
    {% endfor %}
    </table>
    

    Alternatively, you could give your form class a table_print method that returns a html string (wrapped in mark_safe).

    0 讨论(0)
  • 2020-12-31 19:13
    <table>
    {% for i in range(0, len(stuff), 3) %}
        <tr>
        {% for j in range(3) %}
            <td>{{ stuff[i+j] }}</td>
        {% endfor %}     
        </tr>
    {% endfor %}
    </table>
    

    Sorry misunderstood question.

    0 讨论(0)
提交回复
热议问题