title: How would you make a dynamic formset in Django?
body: <p>Here's the way I'm doing it:</p>
  
  <pre><code>{{ formset.management_form }}
  &lt;table&gt;
      {% for form in formset.forms %}
          {{ form }}
      {% endfor %}
  &lt;/table&gt;
  &lt;a href="javascript:void(0)" id="add_form"&gt;Add Form&lt;/a&gt;   
  </code></pre>
  
  <p>And here's the JS:</p>
  
  <pre><code>var form_count = {{formset.total_form_count}};
  $('#add_form').click(function() {
      form_count++;
      var form = '{{formset.empty_form|escapejs}}'.replace(/__prefix__/g, form_count);
      $('#forms').append(form)
      $('#id_form-TOTAL_FORMS').val(form_count);
  });
  </code></pre>
  
  <p>What specifically bothers me is that I had to write that <code>escapejs</code> template tag myself. It just strips all newlines and escapes any single quotes so that it doesn't mess up my string.  But what exactly did the Django makers expect us to do in this situation? And why do they have this <code>TOTAL_FORMS</code> hidden field, when they could have just used an array like <code>&lt;input name="my_form_field[0]" /&gt;</code> and then counted its length instead?</p>