动态添加未发布的Django FormSet数据

Dynamically added Django FormSet data not being posted

本文关键字:Django FormSet 数据 添加 动态      更新时间:2023-09-26

我正在使用JavaScript/jQuery修改一个FormSet,方法是将一个表单动态添加到Django FormSet中。例如,我从一个表格开始询问用户的教育情况。然后,用户可以按添加按钮添加相同的表格,以输入有关中学教育(例如研究生院)的信息。表单被添加到浏览器中,我可以输入数据,但当我POST数据时,它只显示FormSet中的一个表单以及浏览器中第二个表单的信息。

POST数据

edu-0-degree    u'Doctorate'
first_name  u'User' 
last_name   u'One'
Submit  u'Submit'
edu-0-date_started  u'01/01/12'
edu-MIN_NUM_FORMS   u'0'
edu-0-school    u'School Two'
edu-INITIAL_FORMS   u'0'
edu-MAX_NUM_FORMS   u'1000'
edu-0-date_finished u'01/01/16'
edu-0-id    u''
edu-TOTAL_FORMS u'2'
csrfmiddlewaretoken u'qgD2supjYURWoKArWOmkiVRoBPF6Shw0'

然后我得到一个错误说:

CCD_ 1。

以下是相关的代码:

视图.py

def build_profile(request):
    EducationFormset = modelformset_factory(EducationModel, AddEducationForm, extra=1)
    if request.method == "POST":
        education_formset = EducationFormset(request.POST, prefix='edu')
        for form in education_formset:
            if form.is_valid() and form.has_changed():
                education = EducationModel(
                    school = form.cleaned_data['school'],
                    date_started = form.cleaned_data['date_started'],
                    date_finished = form.cleaned_data['date_finished'],
                    degree = form.cleaned_data['degree'],
                    user = current_user
                )
                education.save()
        return HttpResponseRedirect(reverse('private', args=[current_user.username]))
    context = { 
        'edu_formset' : forms['education'],
    }
    return render(request, "build_profile.html", context)

(在这里,我尝试了使用和不使用form.has_changed()片段,结果相同。)

模板build_profile.html

<h2>Education</h2>
{{ edu_formset.management_form }}
{% for form in edu_formset.forms %}
    <div id="{{ form.prefix }}-row" class="dynamic-form">
        {{ form|crispy }}
        <div {% if forloop.first %} class="hidden" {% endif %}>
            <button type="button" class="btn btn-default btn-sm delete-row">
                <span class="glyphicon glyphicon-minus" aria-hidden="true"></span>
            </button>
        </div>
    </div>
{% endfor %}
<div class="btn-group btn-group-xs" role="group" aria-label="...">
    <button type="button" class="btn btn-default add-row">
        <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
    </button>
</div>

build_profile.js(将表单动态添加到FormSet的代码)

function updateElementIndex(el, prefix, ndx) {
    var id_regex = new RegExp('(' + prefix + '-''d+)');
    var replacement = prefix + '-' + ndx;
    if ($(el).attr("for")) $(el).attr("for", $(el).attr("for").replace(id_regex, replacement));
    if (el.id) el.id = el.id.replace(id_regex, replacement);
    if (el.name) el.name = el.name.replace(id_regex, replacement);
}
function addForm(btn, prefix) {
    var formCount = parseInt($('#id_' + prefix + '-TOTAL_FORMS').val());
    var row = $('.dynamic-form:first').clone(true).get(0);
    $(row).removeAttr('id').insertAfter($('.dynamic-form:last')).children('.hidden').removeClass('hidden');
    $(row).children().not(':last').children().each(function() {
        updateElementIndex(this, prefix, formCount);
        $(this).val('');
    });
    $(row).find('.delete-row').click(function() {
        deleteForm(this, prefix);
    });
    $('#id_' + prefix + '-TOTAL_FORMS').val(formCount + 1);
    return false;
}
function deleteForm(btn, prefix) {
    $(btn).parents('.dynamic-form').remove();
    var forms = $('.dynamic-form');
    $('#id_' + prefix + '-TOTAL_FORMS').val(forms.length);
    for (var i=0, formCount=forms.length; i<formCount; i++) {
    $(forms.get(i)).children().not(':last').children().each(function() {
        updateElementIndex(this, prefix, i);
    });
}
return false;
}
$(document).ready( function () {
    $('.add-row').click( function () {
        return addForm(this, 'edu')
    });
    $('.delete-row').click( function () {
        return deleteForm(this, 'edu')
    });
});

我做错了什么?

您得到ValidationError是因为edu_TOTAL-FORMS=2,并且表单集中只有1个表单在您的post-args中。在浏览器中查看源代码,并确保表单名称的前缀正确。看起来这两个表单都有edu-0前缀,当你提交时,只有表单上的最后一个才会被发布。