Hi, I'm the 985th person to attempt dynamic fields
in newforms. ;-)
Actually I've been able to do lots of dynamic stuff
in newforms. It's rendering the forms in templates
that sometimes confuses me. For example, given
the following code to build a form based on an
arbitrary number of medications:
--
from django import newforms as forms
from django.template import Context, Template
class MedForm(forms.Form):
def __init__(self, *args, **kwargs):
super(MedForm, self).__init__(*args, **kwargs)
self.med_list = []
for i in range(1, 4): # arbitrary number
k = 'med_%d' % i
self.fields[k] = forms.CharField(required=False, label=k)
self.med_list.append(self.fields[k])
t = Template("""{% for med in form.med_list %}
{{ med.label }}: {{ med }}
{% endfor %}""")
print t.render(Context({'form':MedForm({})}))
--
Produces the following output:
med_1: <django.newforms.fields.CharField object at 0xb7cd054c>
med_2: <django.newforms.fields.CharField object at 0xb7cd0d2c>
med_3: <django.newforms.fields.CharField object at 0xb7ce0c2c>
Obviously, what I want is something more like this:
med_1: <input type="text" name="med_1" id="id_med_1"/>
med_2: <input type="text" name="med_2" id="id_med_2"/>
med_3: <input type="text" name="med_3" id="id_med_3"/>
So how do I get there? I could do something like
this in line 11:
bf = BoundField(self, self.fields[k], k)
self.med_list.append(bf.as_widget(self.fields[k].widget))
But I'd lose all the field information. Basically, I'm
looking for the magic BoundField.as_widget() rendering
formula. I've put the above code here for easier viewing:
http://dpaste.com/hold/5764/
--
Jeff Bauer
Rubicon, Inc.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---