Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 376
user3623208 Avatar asked May 10 '14 11:05

user3623208


People also ask

How does Django Allauth work?

django-allauth is an integrated set of Django applications dealing with account authentication, registration, management, and third-party (social) account authentication. It is one of the most popular authentication modules due to its ability to handle both local and social logins.


1 Answers

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'})
like image 72
andrea.ge Avatar answered Sep 20 '22 15:09

andrea.ge