[英]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.我需要添加两件事。
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"),
)
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.