title: Submit button for a formset doesn't work when generated by ajax
body: <p>I have a formset to update my models and I display forms from my formset based on the search query. I have an ajax keyup function to send post requests so I can generate search_results.html that I then pass to  in search.html. </p>
  
  <p>Now, if I generate the forms dynamically the submit button does not work. If I ditch the search and pass the content of search_results.html to search.html directly it does work. </p>
  
  <p>I have two views, SearchReportView which supposed to be the main one, and SearchReports to handle generating the report. I guess I might just merge them at this point - it doesn't solve my problem though.</p>
  
  <p>Also I had this working before when I used multiple forms but I had to switch to a formset.</p>
  
  <p>body of search.html</p>
  
  <pre><code>&lt;div class="container"&gt;
  
          &lt;div class="navbar"&gt;
              &lt;div class="navbarItem"&gt;
                  &lt;a href="{% url 'EnbListView' %}"&gt;&lt;img src="{% static 'back.png' %}" width="20px" height="12px" \&gt;Back&lt;/a&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  
      &lt;div&gt;
          &lt;h3&gt;Search:&lt;/h3&gt;
          {% csrf_token %}
          &lt;input type="text" id="search" name="search" /&gt;
                  &lt;span class="button-checkbox"&gt;
                  &lt;button id="mybutton" type="button" class="btn" data-color="primary"&gt;Recent&lt;/button&gt;
                  &lt;input type="checkbox" class="hidden" name="reports_checkbox" checked /&gt;
                  &lt;/span&gt;
      &lt;/div&gt;
  
          &lt;div class="normalText" id="search-results"&gt;&lt;/div&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p>search_results.html</p>
  
  <pre><code>                    {% if reports_and_formset %}
                      &lt;form action="/TP/auto/report/search/" method="post" enctype="multipart/form-data"&gt; {% csrf_token %}
                      {{ formset.management_form }}
                      {% for x, form in reports_and_formset %}
  
                              &lt;button type="submit" class="button" &gt;Submit&lt;/button&gt;
                              {{ form.checking_user }}
                              {{ form.comment }}
                              {% for hidden in form.hidden_fields %}
                                  {{ hidden }}
                              {% endfor %}
  
                      {% endfor %}
                      &lt;/form&gt;
                  {% else %}
                      &lt;tr&gt; &lt;td&gt; &lt;center&gt;
                      No results found.
                      &lt;/center&gt;&lt;/td&gt;&lt;/tr&gt;
                  {% endif %}
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>class SearchReportView(FormView):
  
  def post(self, request, *args, **kwargs):
      # form = ReportCommentForm(initial={'comment': ''})
      # context = {'form': form}
  
      if 'form-TOTAL_FORMS' in request.POST:
  
          single_day_enb_reports = SingleDayEnbReport.objects.all()
  
          report_form_set = modelformset_factory(SingleDayEnbReport, form=ReportCommentForm,
                                                 max_num=single_day_enb_reports.count())
  
          formset = report_form_set(request.POST, queryset=single_day_enb_reports)
  
          formset.save()
  
          return HttpResponseRedirect('/TP/auto/report/search/report_search/')
  
  # @requires_csrf_token
  def get(self, request, *args, **kwargs):
      return render(request, 'app/search.html')
  
  
  class SearchReports(View):
  
  def get(self, request, *args, **kwargs):
  
      single_day_enb_reports = SingleDayEnbReport.objects.all()
  
      report_form_set = modelformset_factory(SingleDayEnbReport, form=ReportCommentForm, max_num=single_day_enb_reports.count())
      formset = report_form_set(queryset=single_day_enb_reports)
  
      single_day_enb_reports_and_formset = zip(single_day_enb_reports, formset)
      # context = {'single_day_enb_reports': single_day_enb_reports, 'formset': formset}
      context = {'reports_and_formset': single_day_enb_reports_and_formset, 'formset': formset}
  
      return render(request, 'app/search_results.html', context, context_instance=RequestContext(request))
  
  @classmethod
  def calculate_dates(self): #not important
  
  def post(self, request, *args, **kwargs):
  
      if 'form-TOTAL_FORMS' in request.POST:
  
          single_day_enb_reports = SingleDayEnbReport.objects.all()
  
          report_form_set = modelformset_factory(SingleDayEnbReport, form=ReportCommentForm,
                                                 max_num=single_day_enb_reports.count())
  
          formset = report_form_set(request.POST, queryset=single_day_enb_reports)
  
          # if formset.is_valid():
          formset.save()
  
          return HttpResponseRedirect('/TP/auto/report/search/report_search/')
  
      if 'button_state' in request.POST:
          search_text = request.POST['search_text']
          button_state = request.POST['button_state']
  
          if button_state == 'false':
              button_state = False
          else:
              button_state = True
  
          if button_state:
              now, fit_date = self.calculate_dates()
              single_day_enb_reports = SingleDayEnbReport.objects.all().filter(
                                                           Q(last_modification_date__range=[fit_date, now]) &amp; (
                                                           Q(alarms__contains=search_text)|
                                                           Q(enb__contains=search_text)|
                                                           Q(creation_date__date__contains=search_text) |
                                                           Q(last_modification_date__date__contains=search_text)|
                                                           Q(comment__contains=search_text)|
                                                           Q(checking_user__name__contains=search_text)))
          else:
              single_day_enb_reports = SingleDayEnbReport.objects.all().filter(
                                                           Q(alarms__contains=search_text)|
                                                           Q(enb__contains=search_text)|
                                                           Q(creation_date__date__contains=search_text) |
                                                           Q(last_modification_date__date__contains=search_text)|
                                                           Q(comment__contains=search_text)|
                                                           Q(checking_user__name__contains=search_text))
  
          report_form_set = modelformset_factory(SingleDayEnbReport, form=ReportCommentForm,
                                                     max_num=single_day_enb_reports.count())
          formset = report_form_set(queryset=single_day_enb_reports)
  
          single_day_enb_reports_and_formset = zip(single_day_enb_reports, formset)
  
          context = {'reports_and_formset': single_day_enb_reports_and_formset, 'formset': formset}
          return render_to_response('app/search_results.html', context, context_instance=RequestContext(request))
  </code></pre>
  
  <p>ajax.js</p>
  
  <pre><code>$('#search').keyup(function(){
  
  delay(function(){
      if($('input[name=reports_checkbox]:checked').val()) button_state = true
      else button_state = false
  
  
          $.ajax({
              type: "POST",
              url: "/TP/auto/report/search/report_search/",
              data: {
                  'button_state' : button_state,
                  'search_text' : $('#search').val(),
                  'csrfmiddlewaretoken' : $("input[name=csrfmiddlewaretoken]").val()
                  },
              success: searchSuccess,
              dataType: 'html'
          });
   }, 500 );
  
  });
  });
  
      function searchSuccess(data, textStatus, jqXHR){
      //generated data is put into search-results &lt;div&gt;field in search.html
      $('#search-results').html(data);
  }
  </code></pre>