1

たくさんのフィールドを持つフォームを表示しようとしています。親フィールドと、親フィールドの下にいくつかの子フィールドがあるように、フィールドをグループ化しようとしています。

だから私がやったことは、子フィールドのリストにアクセスするキーとして親フィールドを持つディクショナリを作成したフォームです。

ここに私のフォームがあります:

class DetailForm(Form):
    a = BooleanField(label='a')
    a1 = BooleanField(label='a1')

    b = BooleanField(label='b')
    b1 = BooleanField(label='b1')
    b2 = BooleanField(label='b2')

    c = BooleanField(label='c')
    c1 = BooleanField(label='c1')    
    c2 = BooleanField(label='c2')
    c3 = ChoiceField(choices=((1,'Default Text'),(0,'Custom Text'),), widget=RadioSelect, label='c3')

    fields_dict = {a: [a1],
                   b: [b1, b2],
                   c: [c1, c2, c3]
                  }

これが私の見解です:

def bfa_report(request, template):
    form = DetailForm()
    fields_dict = form.fields_dict
    return render_to_response(template, {
        'form': form,
        'fields_dict': fields_dict
        }, context_instance=RequestContext(request))

テンプレートで行っていることは次のとおりです。

<div data-dojo-type="dijit/form/Form" id="parameters_form" data-dojo-id="parameters_form" encType="multipart/form-data" action="" method="">
    {% csrf_token %}
    {%  for key, value in fields_dict.items %}
        <div>{{ key }}</div>
        <div>
            {% for val in value %}
                <div>
                    {{ val }}
                </div>
            {% endfor %}
        </div>
    {% endfor %}
</div>

ページに移動すると、ページに次のように表示されます。

a
<django.forms.fields.BooleanField object at 0x7f3aa4444cd0>
b
<django.forms.fields.BooleanField object at 0x7f3aa4442490>
<django.forms.fields.BooleanField object at 0x7f3aa4442d90>
c
<django.forms.fields.BooleanField object at 0x7f3aa4442e10>
<django.forms.fields.BooleanField object at 0x7f3aa4442e90>
<django.forms.fields.ChoiceField object at 0x7f3aa4442f10>

フィールドが表示されません。私がやろうとしていることを行うためのより良い方法があると確信しています。フィールドをグループ化する方法を教えてください。

これを行うための汎用テンプレートを作成しようとしています。表示する必要のあるフォームがいくつかありますが、フォームごとに個別のテンプレートを作成したくありません。

4

1 に答える 1