1

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.

Paulo Bu
  • 29,294
  • 6
  • 74
  • 73
Di Zou
  • 4,469
  • 13
  • 59
  • 88
  • You do not render the instantiated Form, but exactly your declared fields from you fields dict. – Jingo Jun 12 '13 at 15:50

1 Answers1

2

The problem is that you're outputting fields not associated with any forms.

I'll suggest you to create the dictionary overriding the __init__ method of the form:

def __init__(self, *args, **kwargs):
    super(DetailForm, self).__init__(*args, **kwargs)
    self.fields_dict = {self['a']: [self['a1']],
                   self['b']: [self['b1'], self['b2']],
                   self['c']: [self['c1'], self['c2'], self['c3']]
                  }

Hope this helps!

Paulo Bu
  • 29,294
  • 6
  • 74
  • 73
  • Quick question, when I'm retrieving the keys and values using `{% for key, value in fields_dict.items %}`, they're not coming out in the order I want them to. Any way to specify an order? Thanks! – Di Zou Jun 12 '13 at 18:42
  • Aren't they appearing in the order specified in the `__init__`? – Paulo Bu Jun 12 '13 at 18:49
  • I have no idea :( they should've! – Paulo Bu Jun 12 '13 at 19:21
  • Seems that dictionaries get sorted when created. Just checked that in the interpreter :( – Paulo Bu Jun 12 '13 at 19:24
  • Read here and follow the links! http://stackoverflow.com/questions/10371085/python-how-to-disable-auto-sort-when-creating-dictionary – Paulo Bu Jun 12 '13 at 19:26