Overriding Django allauth login form with ACCOUNT_FORMS

前端 未结 1 1658
囚心锁ツ
囚心锁ツ 2021-02-10 02:51

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 =

1条回答
  •  梦如初夏
    2021-02-10 03:21

    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
    

    0 讨论(0)
提交回复
热议问题