Folks, I would appreciate some help figuring out how to create a templated form scenario. The quick form of the question is either:
How do I iterate over two form field list simultaneously in my template using {% for f in form %}, sort of like {% for f,g in form1,form2 %}? or How do I iterate over a list of strings and use that to select form fields in my template? Gory details of and failed attempts follow... I have a dynamically created form, attr_form, that derives its fields from a query and adds pairs of fields to the form roughly like this in my forms.py: def create_attribute_form(): class AttrForm(forms.Form): for every DB Attribute field, attr, on something: name = attr.name # Eg: "Volume" units = "%s-units" % name # Eg: "Volume-units" self.fields[name] = forms.FloatField() # Eg, "17.0" self.fields[units] = forms.ModelChoiceField(...) # Eg, "Liters" In my view, I trump up this form and hand it to my template in a context as "attr_form". I know this form gets to my template correctly because I can simply place {{ attr_form.as_p }} in it and all the fields are present. They are just not how I want them arranged. Specifically, I want to arrange them into a table like this: Attribute | Value | Units -----------------|-------|--------- Temperature | 70 | C Volume | 17 | Liters Which means I need to iterate over "Volume" and "Temperature" attribute names, and locate the "Volume" field and then the "Volume-units" field of my form in order to form one row of the table before going on to the next. If I just iterate over the attr_form straight up, I will see all the attributes, of course. So I kind of want to do this (not working) in my template: with: {{ attr_list }} == [ "Volume", "Temperature" ] {{ attr_form }} == The form instance with pairs of fields in it <table> <th>.....</th> {% for attr in attr_list %} <tr> <td>{{attr}}</td> <td>{{attr_form.fields.$attr.}}</td> <td>{{attr_form.fields."$attr"-"units".}}</td> </tr> {% endfor %} </table> Naturally that $attr and constructing that $attr-"units" thing is totaly bogus. But even if I passed in a list of tuples with ("Volume", "Volume-units") in it and looped over that, I still can't use those for-loop-variables to index into the form's fields[] to get and print the fields! So another approach I tried was this: In my view, create a list of triplets with: ("Volume", <Volume's float form field>, <Volume-units' form field>) and pass that into the template as "attr_trio": <table> <th>.....</th> {% for a,v,u in attr_trio %} <tr> <td>{{a}}</td> <td>{{v}}</td> <td>{{u}}</td> </tr> {% endfor %} </table> This *almost* works, but what I get is a table like: Volume <django.forms.fields.FloatField <django....ModelChoiceField object at 0x2768590> object at 0x2768910> Temperature <django.forms.fields.FloatField <django....ModelChoiceField object at 0x2768790> object at 0x2768050> That looks like the v and u parts are either un-instantiated, or being processed differently, or un-bound, or *something*... As another approach I've explored unsuccessfully so far, I've tried to create two forms: one with the Attribute fields and another with the corresponding Units fields. I can pass both those into the template, but now I need a way to iterate over the attr_form and units_form simultaneously so that I can grab a "Volume" and a "Volume-units" field for one row. I have the itertable list of ["Volume", "Temperature", ...] available, but I don't know how to use that iteration variable name to select the form field from the two forms: with: {{ attr_list }} == [ "Volume", "Temperature" ] {{ attr_form }} == The form instance with just Attribute fields {{ units_form }} == The form instance with corresponding Units fields <table> <th>.....</th> {% for a in attr_list %} <tr> <td>{{a}}</td> <td>{{attr_form.a}}</td> <td>{{units_form.a}}</td> </tr> {% endfor %} </table> Even that glosses over the problem of having a form with two fields labeled with the same name. I'd really have to loop over a tuple of ("Volume", "Volume-units") like: {% for a,u in attrunit_list %} ... <td>{{a}}</td> <td>{{attr_form.a}}</td> <td>{{units_form.u}}</td> But none of that works either. OK. Any chance someone can point out the forehead-slapping obvious solution for me? Or point me in the right direction? Should I give up on doing this in the template and somehow render the whole form into a string in the view and pass that into the template to slap it down? Does that violate some large abstraction separation? :-) Thanks, jdl -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.