Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding Django allauth login form with ACCOUNT_FORMS

I already overrode the signup form with the simple settings variable ACCOUNT_SIGNUP_FORM_CLASS but to override the login form you need to use ACCOUNT_FORMS = {'login': 'yourapp.forms.LoginForm'}. I have the form I want and it displays perfectly with crispy-forms and Bootstrap3:

class LoginForm(forms.Form):
    login = forms.EmailField(required = True)
    password = forms.CharField(widget = forms.PasswordInput, required = True)

    helper = FormHelper()
    helper.form_show_labels = False
    helper.layout = Layout(
        Field('login', placeholder = 'Email address'),
        Field('password', placeholder = 'Password'),
        FormActions(
            Submit('submit', 'Log me in to Cornell Forum', css_class = 'btn-primary')
        ),
    )

When I submit the form I get AttributeError at /account/login/ - 'LoginForm' object has no attribute 'login'. What's going wrong here? The source for the original allauth login form is here: https://github.com/pennersr/django-allauth/blob/master/allauth/account/forms.py

like image 740
wnajar Avatar asked Sep 01 '14 05:09

wnajar


People also ask

How do I override django-Allauth templates?

In order to override templates for django-allauth, you need to create accounts/ directory in your templates folder and add html pages with the names of the pages you want to change.

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

From my understanding, you can overwrite the default LoginForm using ACCOUNT_FORMS, but you need to provide a class that contains all the methods provided in the original class. Your class is missing the login method.

I would set ACCOUNT_FORMS = {'login': 'yourapp.forms.YourLoginForm'} in your settings.py file, where YourLoginForm inherits from the original class.

# 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['password'].widget = forms.PasswordInput()

        # You don't want the `remember` field?
        if 'remember' in self.fields.keys():
            del self.fields['remember']

        helper = FormHelper()
        helper.form_show_labels = False
        helper.layout = Layout(
            Field('login', placeholder = 'Email address'),
            Field('password', placeholder = 'Password'),
            FormActions(
                Submit('submit', 'Log me in to Cornell Forum', css_class = 'btn-primary')
            ),
        )
        self.helper = helper
like image 172
andrea.ge Avatar answered Nov 03 '22 01:11

andrea.ge