简体   繁体   English

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

[英]if statement in Django crispy form, conditional form layout

I have a Django crispy form: A typical sign up form with email address, password field and submit action.我有一个 Django 脆皮表单:一个典型的注册表单,包含电子邮件地址、密码字段和提交操作。

I have a hidden field passed to the Django crispy form from my urls python file called 'billing_secret". The billing secret is different for different URLs.我有一个隐藏字段从我的 urls python 文件传递​​给 Django 脆皮表单,名为“billing_secret”。不同的 URL 的计费秘密是不同的。

objective: To have a terms and conditions radio checkbox enable/disable the submit button for a specific billing secret, consequently url.目标:让条款和条件单选框启用/禁用特定计费机密的提交按钮,因此是 url。

I need to add 2 things.我需要添加两件事。

  1. Add an if statement within the Crispy form to only show a Radio checkbox for a certain billing secret.在 Crispy 表单中添加 if 语句以仅显示特定计费机密的 Radio 复选框。 For example, if the billing secret is "apples" show radios and default to "no".例如,如果计费机密是“apples”,则显示收音机并默认为“no”。 If the billing secret is anything else make the radio hidden, default to yes.如果计费秘密是其他任何使收音机隐藏的东西,默认为是。

This is what I have so far (doesn't work).这是我到目前为止所拥有的(不起作用)。 Apologies I'm completely new to Python.抱歉,我对 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. Disable the submit button when the radio buttons value is ”no" and enable when "yes".当单选按钮值为“no”时禁用提交按钮,当“yes”时启用。

I plan on including this:我计划包括这个:

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

That way a user must agree to the T&C's when signing up before being allowed to submit their details if on on the specified url with the billing secret is “apples”.这样,如果在指定的 url 上使用帐单密码是“apples”,则用户在注册之前必须同意 T&C,然后才能提交他们的详细信息。 If they are on a different url, the radio does not exist and the submit button is enabled.如果它们位于不同的 url 上,则无线电不存在且提交按钮已启用。

Make the button by default hidden: 默认情况下隐藏按钮:

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

And do the checking for the radio button with javascript, when the user click on accept just select the button and show it. 并使用javascript检查单选按钮,当用户单击“接受”时,只需选择按钮并显示它。

EDIT: For conditional elements: 编辑:对于条件元素:

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

Another way to add a conditional layout is with a custom function.添加条件布局的另一种方法是使用自定义函数。

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

Which can then be used in the layout.然后可以在布局中使用。 Pay attention to the asterisk which is needed to convert the return values to separate arguments.请注意将返回值转换为单独参数所需的星号。

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

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

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