Search code examples
pythondjangodjango-formsdjango-templatestemplatetag

How to render form.as_table in multiple columns


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

 <table>
    {{form.as_table}}
 </table>

But the problem is, its rendering all the fields as rows. Since my model contains more than 200 rows, when its showing the elements as 200 rows its looking bad. I want to divide them into 5 columns, so the total rows will be around 40-43. Is there any inbuilt template tag available for this or any other way using which we can solve the problem.


Solution

  • You can refer this doc: looping over form fields

    What you can do in your template is

    {% 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%}