DRY way to declare several similar form fields
<p>Let's say I'm trying to declare a (django) Form class with several FileFields:</p>

<pre><code>class = MyForm(forms.Form):
    file_0 = forms.FileField()
    file_1 = forms.FileField()
    ...
</code></pre>

<p>I have about 20 sequential inputs to declare - what's the best way to avoid typing this all out like a chump?</p>

<p>You can use Django dynamic Form generation</p>

<pre><code>from django import forms

class MyForm(forms.Form):

    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)

        for i in range(20):
            self.fields["file_%d" % i] = forms.FileInput()
</code></pre>

<p>See Docs <a href="https://code.djangoproject.com/wiki/CookBookNewFormsDynamicFields" rel="nofollow">here</a>.</p>

python django