How to render form.as_table in multiple columns

san*_*eep 3 python django django-templates templatetag django-forms

I am using a model form, and I have rendering that form in the template as below.

 <table>
    {{form.as_table}}
 </table>
Run Code Online (Sandbox Code Playgroud)

但问题是,它将所有字段渲染为行。由于我的模型包含 200 多行,因此当将元素显示为 200 行时,看起来很糟糕。我想将它们分成 5 列,因此总行数约为 40-43 行。是否有任何内置模板标签可用于此或任何其他方式,我们可以使用它来解决问题。

Roh*_*han 5

您可以参考此文档:looping over form fields

您可以在模板中执行以下操作

{% for field in form %}

    {%if forloop.counter|divisibleby:"5" %}
        {#after 5 fields, somehow put elements in next row
         of table, paragraph, or using divs etc
        #}
    {%endif%}

    {{field}}
{%endfor%}
Run Code Online (Sandbox Code Playgroud)