gpt4 book ai didi

python - 嵌套的 Django 表单 : 'ManagementForm data is missing or has been tampered with'

转载 作者:搜寻专家 更新时间:2023-10-31 23:07:13 25 4
gpt4 key购买 nike

所以我环顾四周,似乎没有人遇到过我不得不导致这个看似常见的错误的相同问题。我在我的 html 中呈现一些表单,如下所示:

<form method="post" action="">
{{ tags_formset.management_form }}

<!-- code displaying this formset -->
...
<!-- -->

<form method="post" action="">
{{ add_all_form.management_form }}
{{ add_all_form.addTagsToAll }}
<input type="submit" value="Add To Displayed Applicants" />
</form>

<form method="post" action="">
{{ remove_all_form.management_form }}
{{ remove_all_form.removeTagsFromAll }}
<input type="submit" value="Remove From Displayed Applicants" />
</form>
<input type="submit" value="Save Changes" />
</form>

当我没有两个内部表单时,表单集会正确显示并且提交按钮可以提交表单。当我添加第二种形式时,出现了几个问题:

-提交按钮停止工作(尽管在选择表单集的字段之一时按 enter 仍会提交表单

-add_all_form 的提交有效并且功能正常(不是问题,但关于下一点很有趣...)

- remove_all_form 在出现“ManagementForm 数据丢失或已被篡改”验证错误时不起作用。

这是创建表单的 views.py 代码:

    TagsFormSet = formset_factory(TagsForm, formset=TagFormSet, extra=applicantQuery.count())
if request.method == 'POST':
tags_formset = TagsFormSet(request.POST, request.FILES, prefix='tags', applicants=applicantQuery)
add_all_form = TagAddAllForm(request.POST, request.FILES, prefix='addForm', applicants=applicantQuery)
remove_all_form = TagRemoveAllForm(request.POST, request.FILES, prefix='removeForm', applicants=applicantQuery)
redirect = False
if tags_formset.is_valid():
for tagForm in tags_formset.forms:
if 'tags' in tagForm.cleaned_data:
tagForm.saveTags()
if 'removeTags' in tagForm.cleaned_data:
tagForm.deleteTags()
redirect = True
if add_all_form.is_valid():
if 'addTagsToAll' in add_all_form.cleaned_data:
add_all_form.saveTagsToAll()
redirect = True
if remove_all_form.is_valid():
if 'removeTagsFromAll' in remove_all_form.cleaned_data:
remove_all_form.deleteTagsFromAll()
redirect = True
if redirect:
return http.HttpResponseRedirect('')
else:
initForms = []
tags_formset = TagsFormSet(prefix='tags', applicants=applicantQuery)
add_all_form = TagAddAllForm(prefix='addForm', applicants=applicantQuery)
remove_all_form = TagRemoveAllForm(prefix='removeForm', applicants=applicantQuery)

我简直无法弄清楚出了什么问题。我不知道为什么 add_all_form 在 remove_all_form 不起作用时起作用,因为我基本上复制并粘贴了所有涉及的内容(如果需要,我可以从 Forms.py 文件中发布代码,但我认为问题不在那里.. .)

请帮忙!

最佳答案

你应该只使用一个 <form>标签。您可以在这里拥有任意数量的提交按钮,并可以显示任意数量的表单,但所有内容都应该在一个 <form> 中。标签。

然后所有管理数据将在表单提交中正确发送,您的问题应该得到解决。

<form method="post" action="">
{{ tags_formset.management_form }}

<!-- code displaying this formset -->
...
<!-- -->


{{ add_all_form.management_form }}
{{ add_all_form.addTagsToAll }}
<input type="submit" value="Add To Displayed Applicants" />


>
{{ remove_all_form.management_form }}
{{ remove_all_form.removeTagsFromAll }}
<input type="submit" value="Remove From Displayed Applicants" />
<input type="submit" value="Save Changes" />

您的 View 可以保持原样。

关于python - 嵌套的 Django 表单 : 'ManagementForm data is missing or has been tampered with' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16073693/

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