所以我環顧四周,似乎沒有人遇到同樣的問題,我不得不導致這個看似常見的錯誤。我在我的HTML渲染某些形式如下:嵌套的Django表單:'ManagementForm數據丟失或被篡改'
<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>
當我沒有兩個內部形式正確顯示該formset和提交按鈕的作品提交表單。當我將第二兩種形式兩個問題發生:
-The提交按鈕停止工作(雖然在選擇該formset的領域之一併按Enter鍵仍然提交表單
-The add_all_form的提交作品,並功能propperly(不是一個關於下一個點的問題,但有趣的...)
-The 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文件中發佈代碼,但我不認爲問題在那裏.. )
請幫忙!
感謝您的回答!它工作完美。我永遠不會想到我不應該爲每個表單放置表單標籤。它看起來像你會這樣做,我甚至沒有問題。再次感謝! – James 2013-04-19 03:51:41