title: Variable number of inputs with Django forms possible?
body: <p>Is it possible to have a variable number of fields using django forms?</p>
  
  <p>The specific application is this:</p>
  
  <p>A user can upload as many pictures as they want on the image upload form. Once the pictures are uploaded they are taken to a page where they can give the pictures a name and description. The number of pictures will depend on how many the user has chosen to upload.</p>
  
  <p>So <strong>how do I get django to generate a form using a variable number of input fields</strong> (which could be passed as an argument if necessary)?</p>
  
  <p><strong>edit:</strong> a few things have changed since the <a href="http://www.b-list.org/weblog/2008/nov/09/dynamic-forms/" rel="noreferrer">article mentioned in jeff bauer's answer</a> was written. </p>
  
  <p>Namely this line of code which doesn't seem to work:</p>
  
  <pre><code># BAD CODE DO NOT USE!!!
  return type('ContactForm', [forms.BaseForm], { 'base_fields': fields })
  </code></pre>
  
  <p>So here is what I came up with...</p>
  
  <h1>The Answer I used:</h1>
  
  <pre><code>
  from tagging.forms import TagField
  from django import forms
  
  def make_tagPhotos_form(photoIdList):
      "Expects a LIST of photo objects (ie. photo_sharing.models.photo)"
  
      fields = {}
  
      for id in photoIdList:
          id = str(id)
  
          fields[id+'_name'] = forms.CharField()
          fields[id+'_tags'] = TagField()
          fields[id+'_description'] = forms.CharField(widget=forms.Textarea)
  
      return type('tagPhotos', (forms.BaseForm,), { 'base_fields': fields })
  
  </code></pre>
  
  <p>note tagging is not part of django, but it is free and very useful. check it out: <a href="http://code.google.com/p/django-tagging/" rel="noreferrer">django-tagging</a></p>