Django form field grouping

后端 未结 3 435
说谎
说谎 2020-12-30 09:23

Say I have a form with 20 fields, and I want to put 10 of these fields (group1) in a particular div environment and the other 10 fields (group2) in a different div environme

3条回答
  •  既然无缘
    2020-12-30 10:27

    I finally was able to bring @Yuji'Tomita'Tomitas regroup-template-tag-solution to work (see comments in @Yuji'Tomita'Tomitas answer to understand difficulties). I think this is really a nice and easy way to perfom grouping of fields!

    The solution was to regroup via group attribute of field accessing the field attribute of returned BoundFields. Minimal example:

    In forms.py :

    class TestForm(Form):
        a = IntegerField()
        a.group = 1
        b = IntegerField()
        b.group = 1
        c = IntegerField()
        c.group = 2
        d = IntegerField()
        d.group = 2
    

    In template:

    {% csrf_token %} {% regroup form by field.group as field_groups %} {% for field_group in field_groups %} {{field_group.grouper}} {% for field in field_group.list %} {{field}} {% endfor %} {% endfor %}

提交回复
热议问题