gpt4 book ai didi

python - 使用 modelformsets ('ManagementForm data is missing or has been tampered with' 时管理表单错误)

转载 作者:太空狗 更新时间:2023-10-29 22:01:46 25 4
gpt4 key购买 nike

我有一个 models.py 类如下

class Educational_Qualification(models.Model):
user = models.ForeignKey(User)
exam = models.CharField(max_length=40)
pass_month = models.CharField(max_length=40)

我有如下的 views.py

def create_qualification(request):
QFormSet = modelformset_factory(Educational_Qualification, extra=3, exclude=("user",))
if request.method == "POST":
formset = QFormSet(request.POST, request.FILES)

if formset.is_valid():
formset.save()
for form in formset.forms:
if form.is_valid():
quali= form.save(commit=False)
quali.user = request.user
quali.save()

return HttpResponse("Saved")
else:
return HttpResponse("Snafu")
else:
formset = QFormSet()
return render_to_response("register/edu.html", {"formset":formset}, context_instance=RequestContext(request))

当我提交表单时,它会抛出验证错误。声明 ManagementForm 数据丢失或已被篡改'

我的模板中有 formset.management_form。

可能是什么问题?

最佳答案

错误不在您的 View 或模型中,而在模板中。

呈现表单集的正确方法是:

<form method="post" action="">
<table>
{{ formset }}
</table>
</form>

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

我想,您是在循环遍历模板中的表单而不包括管理表单吗?

关于python - 使用 modelformsets ('ManagementForm data is missing or has been tampered with' 时管理表单错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3220420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com