Reputation: 927
I have a Django crispy form: A typical sign up form with email address, password field and submit action.
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.
objective: To have a terms and conditions radio checkbox enable/disable the submit button for a specific billing secret, consequently url.
I need to add 2 things.
This is what I have so far (doesn’t work). Apologies I’m completely new to 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:
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”. If they are on a different url, the radio does not exist and the submit button is enabled.
Upvotes: 4
Views: 3349
Reputation: 2800
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;'))
)
Upvotes: 0
Reputation: 11746
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.
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;'))
Upvotes: 6