{% load i18n %}
    {% if extra_formset.non_form_errors %}<div class='errors'>{{extra_formset.non_form_errors.as_ul}}</div>{% endif %}
    {{ formset.management_form }}
    <table class='inline-table' id='table_{{formset.prefix}}'>
    <caption>{% trans caption %}</caption>
    {% for frm in formset%}{%if forloop.first %}
    <tr>{% for field in frm%}{% if field.label %}
        <th>{{field.label}}</th>{% endif %}{%endfor%}
    </tr>{%endif%}
    <tr id='tr_{{formset.prefix}}_{{forloop.counter0}}'>{% for field in frm.visible_fields %}
        <td>{# Include the hidden fields in the form #}
        {% if forloop.first %}
          {% for hidden in frm.hidden_fields %}
          {{ hidden }}
          {% endfor %}
        {% endif %}
        {% if field.errors %}<div class='errors'>{{ field.errors.as_ul }}</div>{% endif %}
        {{ field }}
        </td>{% endfor %}
    </tr>{%endfor%}
    </table>
    <button id='add_new_{{formset.prefix}}'>{% trans "Add" %}</button>
    <script type='text/javascript'><!--
    $('#add_new_{{formset.prefix}}').click(function(){
        var total_input = $("#id_{{formset.prefix}}-TOTAL_FORMS");
        total_input.val(parseInt(total_input.val())+1);
        var new_line = $('#tr_{{formset.prefix}}_0').clone();
        new_line.find('input').val('');
        new_line.find('input[type=checkbox]').attr('checked', false);
        new_line.find('select').val('');
        {% for field in formset.0.visible_fields %}
        var old_input_id = '{{formset.prefix}}-0-{{field.name}}';
        var input_id = '{{formset.prefix}}-'+(parseInt(total_input.val())-1)+'-{{field.name}}';
        new_line.find('#id_'+old_input_id).attr('id', 'id_'+input_id
                                                ).attr('name', input_id);
        {% endfor %}
        $('#table_{{formset.prefix}}').append(new_line);
        return false;
    });
    // --></script>
