【问题标题】:How does django differentiate between 2 management forms on same page?django 如何区分同一页面上的 2 个管理表单?
【发布时间】:2018-08-20 04:52:33
【问题描述】:

我有 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 如何区分哪个管理表单用于哪个表单集?我怀疑这是导致我的验证错误的原因。有什么想法吗?

【问题讨论】:

  • 抱歉删除我的答案。我看错了你的问题。
  • 相反,您的回答很准确,解决了我的问题。我建议你重新发布你的答案,这样你就可以获得荣誉,并帮助其他遇到同样问题的用户。我的问题是认为前缀适用于表单集中的不同表单实例。当一个表单中有多个表单集时,我们的前缀是必要的。我原以为 django 开发人员会给每个管理表单一个唯一的名称。将是最合乎逻辑的解决方案。
  • 猜我两次太仓促了!感谢您提醒我取消删除。

标签: django validation formset


【解决方案1】:

如果你在同一个&lt;form&gt; 标签中有两个表单(或者,真的,只是在同一个页面上),你应该pass a prefix to the forms,以帮助 django 区分它们。

【讨论】:

  • 我明白了。我认为前缀是为了让 django 区分同一表单集中的实例(如有必要)。我本来希望 django 为每个管理表单创建一个唯一的 ID 以将其标识为特定的表单集,因为每个表单集都有自己的管理表单......听起来这将是最合乎逻辑的事情,不是吗?
猜你喜欢
  • 1970-01-01
  • 2016-03-26
  • 2017-04-22
  • 1970-01-01
  • 1970-01-01
  • 2013-06-12
  • 1970-01-01
  • 1970-01-01
  • 2016-04-06
相关资源
最近更新 更多