django如何在同一页面上区分两种管理形式?

时间:2018-03-12 08:10:02

标签: django validation formset

我有2种基于2种不同型号的表格。

在调试我的表单集失败的验证问题时,is_valid验证(请参阅下面的错误):

(Pdb) FormsetItem.errors
[{'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}]

我注意到当我渲染2个表单集的管理表单时:

{{ FormsetItem.management_form }}
{{ FormsetCat.management_form }}

它为两者创建完全相同的HTML:

<input type="hidden" name="form-TOTAL_FORMS" value="26" id="id_form-TOTAL_FORMS" /><input type="hidden" name="form-INITIAL_FORMS" value="26" id="id_form-INITIAL_FORMS" /><input type="hidden" name="form-MIN_NUM_FORMS" value="0" id="id_form-MIN_NUM_FORMS" /><input type="hidden" name="form-MAX_NUM_FORMS" value="1000" id="id_form-MAX_NUM_FORMS" />
<input type="hidden" name="form-TOTAL_FORMS" value="26" id="id_form-TOTAL_FORMS" /><input type="hidden" name="form-INITIAL_FORMS" value="26" id="id_form-INITIAL_FORMS" /><input type="hidden" name="form-MIN_NUM_FORMS" value="0" id="id_form-MIN_NUM_FORMS" /><input type="hidden" name="form-MAX_NUM_FORMS" value="1000" id="id_form-MAX_NUM_FORMS" />

django如何区分哪种管理形式适用于哪种形式?我怀疑这是导致我的验证错误的原因。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

如果您在同一个<form>标记中有两个表单(或者,实际上,只是在同一页面上),您应该pass a prefix to the forms,以帮助django区分它们。