我的观点:
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)
formset = TimingFormSet(request.POST, prefix='timing')
if form.is_valid() and formset.is_valid():
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 %}
<p>{{form}}</p>
<p>{{formset.management_form}}</p>
<table>
{% for form in formset %}
{{form}}
{% endfor %}
</table>
<input type="submit" value="Create" />
</form>
我想发布一个常规表单和一个表单集。
在这里我收到 ValidationError: u'ManagementForm data is missing or has been tampered with'。我不知道怎么了。需要帮忙。