Python Django crispy表单中的if语句,条件表单布局
我有一个Django crispy表单:一个典型的注册表单,包含电子邮件地址、密码字段和提交操作 我有一个隐藏字段从我的URL python文件传递到Django crispy表单,名为“billing_secret”。不同的URL的计费秘密是不同的 目标: 要设置条款和条件单选复选框,请启用/禁用特定账单密码的提交按钮,然后单击url 我需要补充两件事Python Django crispy表单中的if语句,条件表单布局,python,django,forms,django-crispy-forms,Python,Django,Forms,Django Crispy Forms,我有一个Django crispy表单:一个典型的注册表单,包含电子邮件地址、密码字段和提交操作 我有一个隐藏字段从我的URL python文件传递到Django crispy表单,名为“billing_secret”。不同的URL的计费秘密是不同的 目标: 要设置条款和条件单选复选框,请启用/禁用特定账单密码的提交按钮,然后单击url 我需要补充两件事 在Crispy表单中添加一条if语句,以仅显示某个账单机密的无线电复选框。例如,如果账单机密为“apples”showradios并默认为“n
email = forms.EmailField(label=_("Email"))
password1 = forms.CharField(widget=forms.PasswordInput,label=_("Password"))
billing_secret = forms.CharField()
termsandcond = forms.TypedChoiceField(
label = "Do you agree to the T&C's?",
choices = ((1, "Yes"), (0, "No")),
coerce = lambda x: bool(int(x)),
widget = forms.RadioSelect,
initial = '0',
required = True,
)
def __init__(self, *args, **kwargs):
billing_secret = kwargs.pop('billing_secret', None)
super(RegistrationForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_method = 'post'
self.helper.form_action = '.'
self.helper.layout = Layout(
Field('email', placeholder=_("Email")),
Field('password1', placeholder=_("Password")),
Field('billing_secret', value=billing_secret, type="hidden"),
if billing_secret is 'apples':
return InlineRadios('termsandcond'),
else:
return InlineRadios('termsandcond', initial="1", type="hidden"),
Submit("save", _("Get Started"),css_class="pull-right"),
)
这样,如果指定url上的计费密码为“apples”,则用户必须在注册时同意T&C,然后才能提交其详细信息。如果它们位于不同的url上,则该单选项不存在,并且“提交”按钮已启用。默认情况下隐藏该按钮:
Submit("save", _("Get Started"),css_class="pull-right", style='display: none;')
并使用javascript检查单选按钮,当用户单击accept时,只需选择按钮并显示它
编辑:
对于条件元素:
self.helper.layout = Layout(
Field('email', placeholder=_("Email")),
Field('password1', placeholder=_("Password")),
Field('billing_secret', value=billing_secret, type="hidden"),
)
if billing_secret is 'apples':
self.helper.layout.append(InlineRadios('termsandcond'))
else:
self.helper.layout.append(InlineRadios('termsandcond', initial="1", type="hidden"))
self.helper.layout.append(Submit("save", _("Get Started"),css_class="pull-right", style='display: none;'))
是的,但我需要的是收音机的形式,只有在一个特定的帐单的秘密。所以我的第一个问题是如何根据billing_secret变量有条件地添加表单元素。一旦我明白了这一点,我应该能够使用JS启用/禁用按钮。谢谢你的建议。:)