David Dahan
David Dahan

Reputation: 11162

Django crispy forms: add text next to a checkbox?

It seems to be simple but I can't figure out how to add some text next to my checkbox. I don't want to override the template (I think I don't need to). For now, I have a checkbox that works, but how to put some text like "I Agree TOS" at the right of the checkbox?

Here is what I have: https://www.dropbox.com/s/i3n5rivhzfvo2ms/Screenshot%202015-06-30%2012.10.46.png?dl=0

class TenantSignupForm(forms.Form):
    email = forms.EmailField()
    password = forms.CharField(
        widget=forms.PasswordInput(),
        validators=[RegexValidator(regex=r'[A-Za-z0-9@#$%^&+=]{8,}', code=None),
                    MaxLengthValidator(32)])
    password_confirmation = forms.CharField(
        widget=forms.PasswordInput())
    agree_tos = forms.BooleanField()

    def __init__(self, *args, **kwargs):
        super(TenantSignupForm, self).__init__(*args, **kwargs)
        self.helper = unvariable_helper(self)
        self.helper.form_class = 'm-t'
        self.helper.form_action = 'signup'
        self.helper.layout = Layout(
            Field('email', placeholder="Email"),
            Field('password', placeholder="Password"),
            Field('password_confirmation', placeholder="Confirm your password"),
            Field('agree_tos', wrapper_class='i-checks'),
            FormActions(
                Submit('signup',
                       'Register',
                       css_class='btn btn-primary block full-width m-b'
                      )
                )
        )

Upvotes: 1

Views: 2274

Answers (3)

igo
igo

Reputation: 1787

As for me the best way is using a specific template for this field:

Field('agree_tos', template="{0}/tos.html".format(TEMPLATE_PACK))

Depending on Template_pack used, you have to look at the default template (field.html) in the corresponding directory (say, uni_form or bootstrap) and make your own template quickly and easily.

Upvotes: 0

f43d65
f43d65

Reputation: 2302

I am not familiar with crispy-forms, but may be adding help_text could help

agree_tos = forms.BooleanField(help_text="I Agree TOS")

UPDATE: Another guess

agree_tos = forms.BooleanField(label="I Agree TOS")

Upvotes: 1

Shahul Hameed
Shahul Hameed

Reputation: 404

If you are displaying each Form widget separately in the template, then you could add the required text next to the following widget.

{{ form.agree_tos }} 

Upvotes: 0

Related Questions