Formsets错误[u' ManagementForm数据丢失或已被篡改']

时间:2016-03-09 17:42:32

标签: python django django-forms django-views

我有这个错误[u'ManagementForm data is missing or has been tampered with'],即使management_form在模板中,我在模板中使用了2个表单,我试图在表单集中添加一个前缀,但它没有' t接受前缀。这是我的代码。我不知道我错过了什么,或者我是否需要制作两种形式的表格。 (首次使用formset )。

view.py

def DrugsPrescription(request, id):
    data = RecipeDataForm(prefix='data')
    drugs_formset = formset_factory(DrugsForm)
    patient = PatientData.objects.get(pk=id)
    errors = None
    if request.method == 'POST':
        data = RecipeDataForm(request.POST, prefix='data')
        drugs_formset = drugs_formset(request.POST, prefix="med")
        if data.is_valid():
            info = data.save(commit=False)
            info.patient = patient
            if drugs_formset.is_valid():
                info.save()
                for form in drugs_formset.forms:
                    print form
                med = drugs_formset.save(commit=False)
                med.datos_id = info.pk
                med.save()
                success_url = reverse('/')
                return HttpResponseRedirect(success_url)
            else:
                print drugs_formset.errors
        else:
            print data.errors
    return render(
        request, 'prescription.html',
         {'data': data,
         'patient': patient,
         'drugs_formset': drugs_formset,
         'errors': errors})

template.html

<form class="medical" id="drugs" method="POST" enctype="multipart/form-data">
    {% crispy data %}
    <div>{{errors}}</div>
    <div id="data">
    {{ drugs_formset.management_form }}
    <table border="2">
        {% for form in drugs_formset %}
          <tr>
              {{ form }}
          </tr>
        {% endfor %}
    </table>
</div>

1 个答案:

答案 0 :(得分:0)

当请求不是POST时,您还必须实例化您的表单集。

还要避免对formset类和formset实例使用相同的变量。

您的代码应该是:

def drugs_prescription(request, id):
    DrugsFormSet = formset_factory(DrugsForm)
    patient = PatientData.objects.get(pk=id)
    errors = None
    if request.method == 'POST':
        data = RecipeDataForm(request.POST, prefix='data')
        drugs_formset = DrugsFormSet(request.POST, prefix="med")
        if data.is_valid():
            info = data.save(commit=False)
            info.patient = patient
            if drugs_formset.is_valid():
                info.save()
                for form in drugs_formset.forms:
                    print form
                med = drugs_formset.save(commit=False)
                med.datos_id = info.pk
                med.save()
                success_url = reverse('/')
                return HttpResponseRedirect(success_url)
            else:
                print drugs_formset.errors
        else:
            print data.errors
    else:
        data = RecipeDataForm(prefix='data')
        drugs_formset = DrugsFormSet(prefix='med')
    return render(
        request, 'prescription.html',
         {'data': data,
         'patient': patient,
         'drugs_formset': drugs_formset,
         'errors': errors})

我还将您的函数从DrugsPrescription重命名为drugs_prescription以匹配python样式约定。