django验证错误:您的管理框架数据丢失或已被篡改过'

时间:2014-11-01 09:18:49

标签: django forms formset

它会吃掉我的想法。我创建了一个formset:

TimingFormSet = modelformset_factory(Timing, form=TimingForm, extra=2)

我有一个观点:

class HospitalDoctorAddView(CreateView):

    template_name = "hospital_doctor_add.html"
    form_class = HospitalDoctorInfoForm
    model = HospitalDoctor
    success_url = "/hospital-profile/"

    def get_context_data(self, **kwargs):
        context = super(HospitalDoctorAddView, self).get_context_data(**kwargs)
        context['formset'] = TimingFormSet
        return context

    def post(self, request, *args, **kwargs):

        form = self.form_class(request.POST)
        print "hellooww"
        formset = TimingFormSet(request.POST, prefix='timings')
        print "wow"
        if form.is_valid() and formset.is_valid():
            print "this"
            self.form_save(form)
            self.formset_save(formset)

        return render(request, self.template_name, {
            'form': HospitalDoctorInfoForm,
            'formset': formset})

    def form_save(self, form):
        info = HospitalDoctor()
        info.hospital = self.request.user
        info.full_name = form.cleaned_data['full_name']
        info.expertization = form.cleaned_data['expertization']
        info.nmc_no = form.cleaned_data['nmc_no']
        info.save()

    def formset_save(self, form):
        frm = Timing()
        frm.day = form.cleaned_data['day']
        frm.mng_start = form.cleaned_data['mng_start']
        frm.mng_end = form.cleaned_data['mng_end']
        frm.eve_start = form.cleaned_data['eve_start']
        frm.eve_end = form.cleaned_data['eve_end']
        frm.save()

现在在我的模板中:

<form method="post" action="">{% csrf_token %}
{{form}}
{{formset.management_form}}
<table>
    {% for form in formset %}
       {{form}}
    {% endfor %}
</table>

当我这样做时,它会同时显示表格。但是当我尝试保存或创建它时抛出ValidationError:u'ManagementForm数据丢失或被篡改'。

我不知道什么是错的。真的需要帮助......

1 个答案:

答案 0 :(得分:0)

class HospitalDoctorAddView(CreateView):
    template_name = "hospital_doctor_add.html"
    form_class = HospitalDoctorInfoForm
    model = HospitalDoctor
    success_url = "/hospital-profile/"  # better override get_success_url like below and remove this

    def get_success_url(self):
        return reverse('app_name_namespace:url_name')  # app_name_namespace in project.urls and url_name in your app.urls 

    def get_context_data(self, **kwargs):
        context = super(HospitalDoctorAddView, self).get_context_data(**kwargs)
        context['formset'] = TimingFormSet()

        return context

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        formset = TimingFormSet(request.POST, request.FILES)
        if form.is_valid() and formset.is_valid():
            return self.form_valid(form, formset)
        else:
            return self.form_invalid(form, formset)

    def form_valid(self, form, formset):
        form.save()
        formset.save()

        return HttpResponseRedirect(self.get_success_url())

    def form_invalid(self, form, formset):
        return self.render_to_response(
            self.get_context_data(
                form=form,
                formset=formset,
            )
        )

<强>模板

<form method="post" action="">{% csrf_token %}
    {{form.as_table}}
    <table>
        {{ formset }}
    </table>
</form>