验证表单集

时间:2014-03-11 06:57:40

标签: django formset

我收到此错误:ValidationError at /screen-many/ [u'ManagementForm data is missing or has been tampered with']我认为这是由于我的视图中的后续代码...

# e_pk_list is a list of id's that I got from POST
e_students = Student.objects.filter(pk__in=e_pk_list)
my_iterator = iter(e_students) # Each list item will correspond to a form.
SurveyFormset = formset_factory(SurveyForm, extra=len(e_students))

# Is this the tampering that I can't do??
SurveyFormset.form = staticmethod(curry(SurveyForm, item_iterator=my_iterator))

if request.method == 'POST':

    survey_formset = SurveyFormset(request.POST)

    if survey_formset.is_valid():
        for form in survey_formset:
            saved = form.save(commit=False)
            saved.surveyset = ss
            saved.save()
        return HttpResponseRedirect('/')
else:
    survey_formset = SurveyFormset()

由于

编辑:我想我应该提到我的模板中已经有了一个管理表单....

    <form action="" method="POST">{% csrf_token %}
        {{ survey_formset.management_form }}
        {% for form in survey_formset %}
            <div class="item">
              {% crispy form %}
            </div>
        {% endfor %}
        <input type="submit" value="Submit" class='button' />
    </form>

2 个答案:

答案 0 :(得分:1)

似乎您没有在表单中添加management_form

将此内容放在您正在显示{hm}的SurveyFormset

{{ SurveyFormset.management_form }}

答案 1 :(得分:0)

formset有很多种形式。 Django使用management form data跟踪formset中的表单数量。您也应该在模板中添加management_form,这应该与其他POST数据一起发布。

所以,你应该:

<form method="POST" action=".">
    {{survey_formset.management_form}}
    {% comment %}Other form fields{% endcomment %}
</form>