Django 脆皮形式的 if 语句,条件形式布局

if statement in Django crispy form, conditional form layout

我有一个 Django 脆皮表单:一个带有电子邮件地址、密码字段和提交操作的典型注册表单。

我有一个隐藏字段从我的 urls python 文件传递​​给 Django crispy 表单,名为“billing_secret”。不同 URL 的计费密码不同。

objective: 要有一个条款和条件单选复选框 enable/disable 特定计费秘密的提交按钮,因此 url。

我需要补充两点。

  1. 在 Crispy 表单中添加一个 if 语句,以仅显示特定计费密码的单选复选框。例如,如果计费密码是 "apples" show radios 并且默认为 "no"。如果计费密码是其他任何东西,则隐藏无线电,默认为是。

这是我目前所拥有的(不起作用)。抱歉,我是 Python.

的新手
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"),
    )
  1. 当单选按钮值为“no”时禁用提交按钮并在"yes"时启用。

我打算包括这个:

http://jsfiddle.net/8YBu5/7/

这样,如果在指定的 url 上,用户在注册时必须同意条款和条件,然后才能提交他们的详细信息,并且账单密码是“apples”。如果它们在不同的 url 上,则无线电不存在并且提交按钮已启用。

默认隐藏按钮:

Submit("save", _("Get Started"),css_class="pull-right", style='display: none;')

并使用 javascript 检查单选按钮,当用户单击接受时,仅 select 按钮并显示它。

编辑: 对于条件元素:

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;'))

另一种添加条件布局的方法是使用自定义函数。

def layout_if(condition, *args):
    if condition:
        return args
    else:
        return ()

然后可以在布局中使用。注意将 return 值转换为单独参数所需的星号。

self.helper.layout = Layout(
    Field('email', placeholder=_("Email")),
    Field('password1', placeholder=_("Password")),
    Field('billing_secret', value=billing_secret, type="hidden"),
    *layout_if(billing_secret is 'apples',
               self.helper.layout.append(InlineRadios('termsandcond'))),
    *layout_if(billing_secret is not 'apples',
               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;'))
)