在初始创建/显示后动态更新 Django 中的表单
是否可以在创建并显示 Django 表单后动态修改和验证它。(我找到了一些显示动态表单创建的片段,但这些片段要求在创建表单之前已知/定义动态字段。)要求不一样。
使用案例: 我有一个表单,我想根据初始表单的下拉列表中的选择来显示和验证其他输入字段。根据选择,1) 添加其他字段,2) 必须通过适当的错误处理进行验证。
(如果您想知道要添加的数据元素是以 csv 格式存储在模型中的一组名称/值对,但显示时它们显示为单独的输入字段 - 输入数据将先转换为 csv 字符串以节省)。
我使用 ajax 动态地将附加字段添加到模板中,部分实现了这一点,但还没有找到验证这些新字段的方法。我不确定这是否是一种可行的方法。我可能可以使用 javascript 在浏览器中执行此操作,但更喜欢 Django/服务器端解决方案。
Is there away to dynamically modify and validate a Django form AFTER it's been created and displayed.(I have found a few snippets that show dynamic form creation, but these require that the dynamic fields are known/defined prior to creating the form.) My requirement is different.
Use Case:
I have a form where I want to display and validate additional input fields based on the selection from a dropdown on the initial form. Based on the selection additional fields are 1) added and 2)must then be validated with appropriate error handling.
(In case you are wondering the data elements to be added are a set of name/value pairs stored in csv format in the model, but when displayed they are shown as separate input fields - the input data will be converted to a csv string prior to saving).
I got this partially working using ajax to dynamically add the additional fields to a template, but have not found a way to validate these new fields. I'm not sure if this is a workable approach. I could probably do this in the browser using javascript, but would prefer a Django/server side solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的 - 所以到目前为止没有人回答我的问题。然而,我做了更多的谷歌搜索,发现了一篇优秀的文章,描述了我正在寻找的解决方案。工藤少校向本博客的作者致谢。
使用 JQuery 创建 Django 动态表单 - 动态字段添加和删除
OK - So none responded to my question so far. However I did a bit more googling and found an excellent article that describe the solution I was looking for. Major Kudo's to the author of this blog.
Create a Django Dynamic Form with JQuery - Dynamic Field Addition and Removal