title: Django formset : Save multiple forms from formset
body: <p>I spent several days in order to <code>add dynamically forms to formset</code> and I find a way to do that. But I have a little issue with <code>forms saving</code> because it just saves the first form and not all forms from formset.</p>
  
  <p>This is my formset :</p>
  
  <pre><code>DocumentFormSets = inlineformset_factory(Publication, Document, form=DocumentForm, extra=1, max_num=4)
  </code></pre>
  
  <p>This is my template :</p>
  
  <pre><code>&lt;fieldset&gt;
      &lt;legend class="title"&gt;&lt;span class="name"&gt;{% trans 'Document form' %}&lt;/span&gt;&lt;/legend&gt;
      {{ DocumentFormSet.management_form }}
      &lt;div id="form_set"&gt;
        {% for form in DocumentFormSet.forms %}
          &lt;div class='formset-document'&gt;
            &lt;table class='no_error'&gt;
              &lt;div class="row"&gt;
                &lt;div class="col-xs-3"&gt;
                  {{ form.title|as_crispy_field }}
                &lt;/div&gt;
                &lt;div class="col-xs-3"&gt;
                  {{ form.language|as_crispy_field }}
                &lt;/div&gt;
                &lt;div class="col-xs-3"&gt;
                  {{ form.format|as_crispy_field }}
                &lt;/div&gt;
                &lt;div class="col-xs-3"&gt;
                  {{ form.upload|as_crispy_field }}
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/table&gt;
          &lt;/div&gt;
        {% endfor %}
      &lt;/div&gt;
      &lt;br&gt;
      &lt;input type="button" class="btn btn-default" value="Add More" id="add_more"&gt;
      &lt;div id="empty_form" style="display:none"&gt;
        &lt;table class='no_error'&gt;
          &lt;div class="row"&gt;
            &lt;div class="col-xs-3"&gt;
              {{ DocumentFormSet.empty_form.title|as_crispy_field }}
            &lt;/div&gt;
            &lt;div class="col-xs-3"&gt;
              {{ DocumentFormSet.empty_form.language|as_crispy_field }}
            &lt;/div&gt;
            &lt;div class="col-xs-3"&gt;
              {{ DocumentFormSet.empty_form.format|as_crispy_field }}
            &lt;/div&gt;
            &lt;div class="col-xs-3"&gt;
              {{ DocumentFormSet.empty_form.upload|as_crispy_field }}
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/table&gt;
      &lt;/div&gt;
  &lt;/fieldset&gt;
  </code></pre>
  
  <p>This is my JS part :</p>
  
  <pre><code>$('#add_more').click(function () {
    var form_idx = $('#id_form-TOTAL_FORMS').val();
    $('#form_set').append($('#empty_form').html().replace(/__prefix__/g, form_idx));
    $('#id_form-TOTAL_FORMS').val(parseInt(form_idx) + 1);
  });
  </code></pre>
  
  <p>And this is my views.py file :</p>
  
  <pre><code>class PublicationCreateView(EdqmCreateView):
      """ Create publication with document form through formset """
      model = Publication
      template_name = 'freepub/publication/publication_form.html'
  
      def get_context_data(self, **kwargs):
          context = super(PublicationCreateView, self).get_context_data(**kwargs)
          context['DocumentFormSets'] = DocumentFormSets(self.request.POST or None, self.request.FILES or None)
          return context
  
      def form_valid(self, form):
          context = self.get_context_data()
          formsets = context['DocumentFormSets']
          if form.is_valid() and formsets.is_valid():
              self.object = form.save()
              formsets.instance = self.object
              formsets.save()
          return super(PublicationCreateView, self).form_valid(form)
  </code></pre>
  
  <p>But with this function, it saves only the first document form in my formset : </p>
  
  <p>I looked over id document form field and I get this :</p>
  
  <p><a href="https://i.stack.imgur.com/SvE2C.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SvE2C.gif" alt="enter image description here"></a></p>
  
  <p>The first title field get <code>id_documents-0-title</code> but when I add more forms, title field get always <code>id_documents-undefined-title</code></p>
  
  <p>How I can save my document forms from formsets ?</p>
  
  <p>Thank you !</p>
  
  <p><strong>EDIT :</strong></p>
  
  <p>This is an edit from @HaiLang question :</p>
  
  <p>This is my <code>self.request.POST</code> :</p>
  
  <pre><code>&lt;QueryDict: {'csrfmiddlewaretoken': ['0LHaaEG1dkyZmOnP3X7037tZI45QrQOIRrQQAMhiOoTyeohDwQdcdTt08yuqH86Z'], 'category': ['23'], 'pub_id': ['PUBSDDD-55'], 'title': ['TESTPUBLIE'], 'description': [''], 'doc-TOTAL_FORMS': ['1'], 'doc-INITIAL_FORMS': ['0'], 'doc-MIN_NUM_FORMS': ['0'], 'doc-MAX_NUM_FORMS': ['4'], 'doc-0-title': ['doc1'], 'doc-0-language': ['FR'], 'doc-0-format': ['pdf'], 'doc-undefined-title': ['doc2'], 'doc-undefined-language': ['FR'], 'doc-undefined-format': ['pdf'], 'doc-__prefix__-title': [''], 'doc-__prefix__-language': [''], 'doc-__prefix__-format': [''], 'doc-__prefix__-upload': ['']}&gt;
  </code></pre>
  
  <p>And this is my <code>console.log</code> :</p>
  
  <pre><code>Element kt.fn.init {}
  </code></pre>