Django: create HTML input array using a django form

It should be more like e.g.:

# in a model class
for i in range(1, prim+1):
    self.fields['asdf_%s' % i] = forms.CharField(label='Label %i' % i)

But it very depends on what you want to achieve.


It looks like I can do what I need to do by breaking the form into multiple formsets...

http://docs.djangoproject.com/en/dev/topics/forms/formsets/#topics-forms-formsets

Then, I should be able to access each formset individually from the template, wrapping all of them into one


Jacob Kaplan-Moss (co-author of Django) recently posted a great article for handling dynamic forms, which should solve your problem in a preferred way: http://jacobian.org/writing/dynamic-form-generation/

He's using the same method that Felix suggests, but it's worth reading the whole article to get a better grasp on the concept.

Using the asdf[] technique is sloppy, because then you have to deal with ordering. It's also not the standard practice.

Edit:

To handle the situation where you need to detect when you hit these dynamic fields:

{% for input in form.fields %}
    {% ifequal input.label 'asdf' %}
        {{ forloop.counter }}: {{input}}<br />
    {% endifequal %}
{% endfor %}