Answer a question

I am trying to automate the creation of something like this:

<input type='text' name='asdf[]' />
<input type='text' name='asdf[]' />
<input type='text' name='asdf[]' />

By cycling through a range in the form. I've been trying things like this, along with several other variations:

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

# in the template
<form action='#' method='post'>
    {{form.as_p}}
</form>

But I haven't had any luck though.

How can I go about automating an array of inputs?

** edit ** To clarify, eventually I need to be able to access the fields in the template like this:

{% for input in form.fields.asdf %}
{{input}}
{% endfor %}

Which would then hopefully get me the original input list shown above...

Answers

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

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐