所以我环顾四周,似乎没有人遇到过我不得不导致这个看似常见的错误的问题。我在我的 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 的提交工作正常(不是问题,但关于下一点很有趣......)
- 由于“ManagementForm 数据丢失或已被篡改”验证错误,remove_all_form 无法正常工作。
这是创建表单的 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 文件中发布代码,但我认为问题不存在.. .)
请帮忙!