Search code examples
pythondjangoformsdjango-crispy-forms

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.

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.

  1. Add an if statement within the Crispy form to only show a Radio checkbox for a certain billing secret. For example, if the billing secret is "apples" show radios and default to "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.

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".

I plan on including this:

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”. If they are on a different url, the radio does not exist and the submit button is enabled.


Solution

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