本文介绍了django modelformset_factory - 管理表单数据丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 u'ManagementForm数据丢失或已被篡改 > 那就是我的代码:请指出我的错误并帮助我解决这个问题。 @csrf_protect @ transaction.commit_on_success def signup(request): form = NewUserCreationForm() doc_form = NewDocRegisterForm() SpecialityLicensesFormSet = modelformset_factory(SpecialityLicenses,extra = 1,exclude =('user')) formset = SpecialityLicensesFormSet(queryset = SpecialityLicenses.objects.none()) 如果有请求。方法==POST: $ b $ form = NewUserCreationForm(request.POST或None) doc_form = NewDocRegisterForm(request.POST或None) formset = SpecialityLicensesFormSet(request.POST或无) 如果form.is_valid()和doc_form.is_valid()和formset.is_v alid(): user = form.save() doc = doc_form.save(commit = False) doc.user = user doc.save() $ b printsetset fset = formset.save(commit = False) for fset: n.user = user n.save() return HttpResponse(Uzytkownik utowrzony) return render_to_response(userena / signup_new。 html',{'form':form,'doc_form':doc_form,$ b $'spec_form':formset,}, context_instance = RequestContex t(request) ) 模板代码: < form action =/ en / accounts / doc_register /method =post> {%csrf_token%} {%for field in form%} < DIV> {%if field.errors%} {{field.errors | striptags}} | {%endif%} {{field.label}} | {{field}} < / div> {%endfor%} < hr> {%for f in doc_form%} < div> {%if f.errors%} {{f.errors | striptags}} | {%endif%} {{f.label}}:{{f}} < / div> {%endfor%} < hr> {{spec_form.management_form}} {{spec_form}} < hr> < input type =submitvalue =Submit/> < / form> {%endblock%} 解决方案如果你做了 {{formset}} ,就不需要做 {{formset.management_form}} ,只要你做 $ $ p $ code $ {formset.management_form}} {%for formset%} {{请尝试删除 位。查看第三个示例 I am still fighting with formsets and I cant really understand why I am getting this error:u'ManagementForm data is missing or has been tampered withThats my code:Please point out my mistakes and help me with resolving this issue.@[email protected]_on_successdef signup(request): form = NewUserCreationForm() doc_form = NewDocRegisterForm() SpecialityLicensesFormSet = modelformset_factory(SpecialityLicenses, extra=1, exclude = ('user')) formset = SpecialityLicensesFormSet(queryset=SpecialityLicenses.objects.none()) if request.method == "POST": form = NewUserCreationForm(request.POST or None) doc_form = NewDocRegisterForm(request.POST or None) formset = SpecialityLicensesFormSet(request.POST or None) if form.is_valid() and doc_form.is_valid() and formset.is_valid(): user = form.save() doc = doc_form.save(commit=False) doc.user = user doc.save() print formset fset = formset.save(commit=False) for n in fset: n.user = user n.save() return HttpResponse("Uzytkownik utowrzony") return render_to_response("userena/signup_new.html", {'form': form, 'doc_form': doc_form, 'spec_form': formset,}, context_instance=RequestContext(request)) Template code:<form action="/en/accounts/doc_register/" method="post">{% csrf_token %} {% for field in form %} <div> {% if field.errors %} {{ field.errors|striptags }} | {% endif %} {{field.label}} | {{ field}} </div> {% endfor %} <hr> {% for f in doc_form %} <div> {% if f.errors %} {{f.errors|striptags}} | {% endif %} {{f.label}} : {{ f }} </div> {% endfor %} <hr> {{ spec_form.management_form }} {{ spec_form }} <hr> <input type="submit" value="Submit"/></form>{% endblock %} 解决方案 you don't need to do {{ formset.management_form }} if you do {{ formset }}, just if you do {{ formset.management_form }}{% for form in formset %} {{ form }}{% endfor %}try removing the {{ spec_form.management_form }} bit from your template. Look at the third example 这篇关于django modelformset_factory - 管理表单数据丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-22 12:43