django形式的条件逻辑 [英] Conditional Logic in django forms

查看:139
本文介绍了django形式的条件逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建条件窗体,其中选择一个选项会导致新字段出现在下面,而选择另一个选项则会显示其他选项。

I'm trying to build a conditional form where selecting one option will cause a new field to appear underneath, while selecting another option will display something else.

Formstack有一个很好的例子 https:/ /www.formstack.com/features/conditional-logic

Formstack has a good example https://www.formstack.com/features/conditional-logic

我没有找到任何具有此功能的django的预先存在的表单包。我应该如何开始实现?

I couldn't find any preexisting form packages for django with this functionality. How should I start implementing this?

推荐答案

Django表单(特别是如果你使用 ModelForm library)直接反映了您的Django应用程序 Model 。因此,您应该首先重构您的Django应用程序 Model 以具有可选值的字段(即它们可以是 NULL 或已经创建了一个默认值值。

Django forms (especially if you use the ModelForm library) are a direct reflection of your Django application Model. You should therefore start by refactoring your Django application Model to have fields that have optional values (i.e. they can be NULL, empty or have a default value already created).

这些将是根据您的条件显示/隐藏的表单字段,并且它们可能有也可能没有值(如果它们是根据条件隐藏的,那么不可能为它们提供一个值,因此模型字段必须能够接受 NULL 值或使用默认值。

These would be the form fields that are shown/hidden based on your conditional(s) and they may or may not have values (if they are hidden based on a conditional it is impossible to provide a value to them so the Model fields must be able to accept NULL values or use the defaults).

然后,您将使用客户端语言(如JavaScript(JS))来处理与Django应用程序的用户迭代。一个简单的使用JS框架,如 jQuery 将值得您根据自己的需要进行调查。

You would then use a client-side language such as Javascript (JS) to handle the user iteraction with your Django application. A simple to use JS framework like jQuery would be worthwhile investigating for your needs.

除了表单上的Django文档外,我还强烈建议您查看由PyDanny发表的 Django Crispy Forms 看看Django表单应该如何做。

In addition to the exceptional Django docs on Forms, I also highly recommend you take a look at Django Crispy Forms writtten by PyDanny to see how Django forms should be done right.

这篇关于django形式的条件逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆