user3623208
user3623208

Reputation: 51

Overwrite django-allauth form field

I want to modify attributes of a form field. Specifically, the login form:

(django-allauth LoginForm)

class LoginForm(forms.Form):

password = PasswordField(label=_("Password"))
remember = forms.BooleanField(label=_("Remember Me"),
                              required=False)

user = None

def __init__(self, *args, **kwargs):
    super(LoginForm, self).__init__(*args, **kwargs)
    if app_settings.AUTHENTICATION_METHOD == AuthenticationMethod.EMAIL:
        login_widget = forms.TextInput(attrs={'type': 'email',
                                              'placeholder':
                                              _('E-mail address'),
                                              'autofocus': 'autofocus'})
        login_field = forms.EmailField(label=_("E-mail"),
                                       widget=login_widget)
    elif app_settings.AUTHENTICATION_METHOD \
            == AuthenticationMethod.USERNAME:
        login_widget = forms.TextInput(attrs={'placeholder':
                                              _('Username'),
                                              'autofocus': 'autofocus'})
        login_field = forms.CharField(label=_("Username"),
                                      widget=login_widget,
                                      max_length=30)
    else:
        assert app_settings.AUTHENTICATION_METHOD \
            == AuthenticationMethod.USERNAME_EMAIL
        login_widget = forms.TextInput(attrs={'placeholder':
                                              _('Username or e-mail'),
                                              'autofocus': 'autofocus'})
        login_field = forms.CharField(label=pgettext("field label",
                                                     "Login"),
                                      widget=login_widget)
    self.fields["login"] = login_field
    set_form_field_order(self,  ["login", "password", "remember"])

How I overwrite (or override) a django-allauth form field? Help!

Upvotes: 5

Views: 3109

Answers (3)

Milovan Tomašević
Milovan Tomašević

Reputation: 8673

class SignupForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
    super(SignupForm, self).__init__(*args, **kwargs)
    self.fields['first_name'].widget = forms.TextInput(attrs={'placeholder': 'Enter first name'})
    self.fields['last_name'].widget = forms.TextInput(attrs={'placeholder': 'Enter last name'})

#settings.py or base.py    
ACCOUNT_SIGNUP_FORM_CLASS = 'NameApp.forms.SignupForm'

Upvotes: 0

andrea.ge
andrea.ge

Reputation: 1987

You can overwrite the default LoginForm using ACCOUNT_FORMS in your settings.py, for example:

ACCOUNT_FORMS = {'login': 'yourapp.forms.YourLoginForm'}

and write a YourLoginForm accordingly.

# yourapp/forms.py

from allauth.account.forms import LoginForm

class YourLoginForm(LoginForm):
    def __init__(self, *args, **kwargs):
        super(YourLoginForm, self).__init__(*args, **kwargs)
        self.fields['login'].widget = forms.TextInput(attrs={'type': 'email', 'class': 'yourclass'})
        self.fields['password'].widget = forms.PasswordInput(attrs={'class': 'yourclass'})

Upvotes: 10

wnajar
wnajar

Reputation: 757

I know you can override the signup form class with the ACCOUNT_SIGNUP_FORM_CLASS settings variable... but as far as I know there is no way to change the login form. I asked my own similar question here.

Upvotes: 0

Related Questions