Django-allauth: How to disable input field labels on sign up and log in page?

Created on 14 Aug 2017  ·  7Comments  ·  Source: pennersr/django-allauth

What is the easiest way to disable the input labels on the sign up and log in page?

Most helpful comment

The easiest?

label {
    display: none;
}

;)

All 7 comments

The easiest?

label {
    display: none;
}

;)

And if I want to disable it instead of hiding it?

Input fields can be disabled, but I am not sure what you mean by disabling a label. In any case, the templates included in allauth merely serve as a starting point. Simply copy them and adjust them as needed for your own project...

With “disabling” I meant disabling it in allauth settings in order to remove it instead of hiding it using CSS. I tried copying the templates, but as far as I know the labels are created in forms.py instead of the template files. Any idea how I can get rid of it?

This is the function that I need to override. I need to delete all 'label=_' references. I've created a forms.py file in my 'authentication' app and added ACCOUNT_SIGNUP_FORM_CLASS = 'authentication.forms.MySignupForm' to settings.py. Is this the correct way to do this? Can I override BaseSignupForm without overriding SignupForm?

class BaseSignupForm(_base_signup_form_class()):
    username = forms.CharField(label=_("Username"),
                               min_length=app_settings.USERNAME_MIN_LENGTH,
                               widget=forms.TextInput(
                                   attrs={'placeholder':
                                          _('Username'),
                                          'autofocus': 'autofocus'}))
    email = forms.EmailField(widget=forms.TextInput(
        attrs={'type': 'email',
               'placeholder': _('E-mail address')}))

    def __init__(self, *args, **kwargs):
        email_required = kwargs.pop('email_required',
                                    app_settings.EMAIL_REQUIRED)
        self.username_required = kwargs.pop('username_required',
                                            app_settings.USERNAME_REQUIRED)
        super(BaseSignupForm, self).__init__(*args, **kwargs)
        username_field = self.fields['username']
        username_field.max_length = get_username_max_length()
        username_field.validators.append(
            validators.MaxLengthValidator(username_field.max_length))
        username_field.widget.attrs['maxlength'] = str(
            username_field.max_length)

        default_field_order = [
            'email',
            'email2',  # ignored when not present
            'username',
            'password1',
            'password2'  # ignored when not present
        ]
        if app_settings.SIGNUP_EMAIL_ENTER_TWICE:
            self.fields["email2"] = forms.EmailField(
                label=_("E-mail (again)"),
                widget=forms.TextInput(
                    attrs={
                        'type': 'email',
                        'placeholder': _('E-mail address confirmation')
                    }
                )
            )
        if email_required:
            self.fields['email'].label = ugettext("E-mail")
            self.fields['email'].required = True
        else:
            self.fields['email'].label = ugettext("E-mail (optional)")
            self.fields['email'].required = False
            self.fields['email'].widget.is_required = False
            if self.username_required:
                default_field_order = [
                    'username',
                    'email',
                    'email2',  # ignored when not present
                    'password1',
                    'password2'  # ignored when not present
                ]

        if not self.username_required:
            del self.fields["username"]

        set_form_field_order(
            self,
            getattr(self, 'field_order', None) or default_field_order)

    def clean_username(self):
        value = self.cleaned_data["username"]
        value = get_adapter().clean_username(value)
        return value

    def clean_email(self):
        value = self.cleaned_data['email']
        value = get_adapter().clean_email(value)
        if value and app_settings.UNIQUE_EMAIL:
            value = self.validate_unique_email(value)
        return value

    def validate_unique_email(self, value):
        return get_adapter().validate_unique_email(value)

    def clean(self):
        cleaned_data = super(BaseSignupForm, self).clean()
        if app_settings.SIGNUP_EMAIL_ENTER_TWICE:
            email = cleaned_data.get('email')
            email2 = cleaned_data.get('email2')
            if (email and email2) and email != email2:
                self.add_error(
                    'email2', _("You must type the same email each time.")
                )
        return cleaned_data

    def custom_signup(self, request, user):
        custom_form = super(BaseSignupForm, self)
        if hasattr(custom_form, 'signup') and callable(custom_form.signup):
            custom_form.signup(request, user)
        else:
            warnings.warn("The custom signup form must offer"
                          " a `def signup(self, request, user)` method",
                          DeprecationWarning)
            # Historically, it was called .save, but this is confusing
            # in case of ModelForm
            custom_form.save(user)

Questions on tweaking form rendering is not allauth specific -- solution vary ranging from rendering the forms manually to using one of the many template tag libraries for adding classes and tweaking the overall form output. Alternatively, you could override all the forms and tweak the widgets that way, though I personally prefer to keep my python code free from styling/display issues.

Alternatively, you could override all the forms and tweak the widgets that way, though I personally prefer to keep my python code free from styling/display issues.

I don't understand this part. Why would it be a problem to remove the labels in the Python code?
The labels are initialised in forms.py, which is Python. I think a combination of labels and place holder text on login and sign up page is overkill and it looks ugly.

Could you please explain me how I can override BaseSignupForm and SignupForm, since both functions initialise labels?

Was this page helpful?
0 / 5 - 0 ratings