I am trying to display a form with a bunch of fields. I am trying to group the fields together so there is a parent field and then some child fields underneath the parent field.
So what I have done is in my form created a dictionary with the parent fields as keys that access a list of the child fields.
Here is my form:
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]
}
Here is my view:
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))
Here is what I'm doing in my template:
<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>
When I go to the page, I end up with this displayed on the page:
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>
The fields aren't showing up. I'm sure there is a better way to do what I'm trying to do. How do I get the fields grouped the way I them to?
I'm trying to create a generic template to do this. I have several forms that I need displayed, and I don't want to create a separate template for each form.