简体   繁体   English

担心django中的表单向导:AJAX,条件

[英]Worries about form wizards in django: AJAX, conditionals

I have a set of pretty complex forms that I'd like to implement in django. 我有一套非常复杂的表单,我想在django中实现。 Several of the later fields depend on the values in the earlier fields, and the validation rules are very complicated. 后面的几个字段取决于早期字段中的值,验证规则非常复杂。

For example, suppose the form is helping users file state tax returns. 例如,假设表单正在帮助用户提交州税申报表。 On the first page, I might have a select box where users choose a state (Alabama, Arkansas, Alaska...). 在第一页上,我可能会有一个用户选择州的选择框(阿拉巴马州,阿肯色州,阿拉斯加州......)。 Depending on which state gets chosen, the next page will ask completely different questions, since taxes are so different across states. 根据选择的状态,下一页将提出完全不同的问题,因为各州的税收差别很大。

This seems like the kind of thing that form wizards ought to be good at, but I've never done a django wizard before, and I have some worries. 这似乎是巫师应该擅长的那种东西,但我以前从未做过django巫师,我有些担心。

First, do form wizards work with AJAX? 首先,表单向导是否适用于AJAX? Specifically, I'd like to load and render the form (and all its steps) within in a jquery tabs object. 具体来说,我想在jquery选项卡对象中加载和呈现表单(及其所有步骤)。 As each step is completed, the next page of the form would be dynamically loaded, without reloading the whole page. 每个步骤完成后,表单的下一页将动态加载,无需重新加载整个页面。

Second, AFAICS, conditional view/skip patterns in django's form wizard only do booleans. 其次,AFAICS,django的表单向导中的条件视图/跳过模式只做布尔值。 One key part of my form flow is the long-ish select input: choosing the state. 我的表单流程的一个关键部分是长期选择输入:选择状态。 Form wizards don't look like they can handle that kind of pattern very gracefully. 表单向导看起来不像他们可以非常优雅地处理这种模式。 Is there a good workaround or alternative? 有没有好的解决方法或替代方案?

On the whole, are form wizards the right way to implement this kind of structure? 总的来说,形式向导是实现这种结构的正确方法吗? Using jquery alone seems unhandy, since I'd (presumably) have to load all 50 state-specific forms together at the beginning. 单独使用jquery似乎是不方便的,因为我(大概)必须在开始时将所有50个特定于状态的表单加载在一起。

I don't think form wizard is exactly what you need (the fact that the interface looks like a wizard doesn't necessarily map over django's wizard concept, especially because you have somewhat dynamic forms). 我不认为表单向导正是你所需要的(事实上,界面看起来像向导不一定映射到django的向导概念,特别是因为你有一些动态形式)。 From the form wizard docs: 从表单向导文档:

splits forms across multiple Web pages. 在多个网页上拆分表单。 It maintains state in one of the backends so that the full server-side processing can be delayed until the submission of the final form. 它在其中一个后端维护状态,以便可以延迟完整的服务器端处理,直到提交最终表单。

My take would be to do multiple forms and initially have them work without too much javascript (ok; you can, if you want, load the states using some autocompletion API). 我的看法是做多个表单,最初让它们工作没有太多的javascript(好吧;你可以,如果你愿意,可以使用一些自动完成API加载状态)。 Then, you can try eg pjax . 然后,你可以尝试例如pjax

In the end, the answers to my questions were "No," and "No." 最后,我的问题的答案是“不”和“不”。 Standard formWizards just don't do AJAX and complex conditionals gracefully, so I ended up writing my own set of classes to do it. 标准表单向导只是不优雅地执行AJAX和复杂条件,所以我最终编写了自己的类来完成它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM