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

前端 未结 2 851
不知归路
不知归路 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:

    
    {% for field in form %}
       
       {% if forloop.last %}
         
       {% else %}
         {% if forloop.counter|divisibleby:"3" %}
           
         {% endif %}
       {% endif %}
    {% endfor %}
    
    {{ field }}

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

提交回复
热议问题