覆盖使用ACCOUNT_FORMS的Django allauth登录表单 [英] Overriding Django allauth login form with ACCOUNT_FORMS

查看:479
本文介绍了覆盖使用ACCOUNT_FORMS的Django allauth登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用简单的设置变量 ACCOUNT_SIGNUP_FORM_CLASS 覆盖了注册表单,但要覆盖您需要使用的登录表单 ACCOUNT_FORMS = {'login' :'yourapp.forms.LoginForm'} 。我有我想要的形式,它完美地显示了脆弱的形式和Bootstrap3:

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')
        ),
    )

当我提交表单时,我得到 AttributeError在/ account / login / - 'LoginForm'对象没有属性登录。这里出了什么问题?原始allauth登录表单的来源在此: https: //github.com/pennersr/django-allauth/blob/master/allauth/account/forms.py

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

推荐答案

<根据我的理解,您可以使用 ACCOUNT_FORMS 覆盖默认的LoginForm,但是您需要提供一个包含原始类中提供的所有方法的类。您的课程缺少登录方法。

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.

我将设置 ACCOUNT_FORMS = {'login ':'yourapp.forms.YourLoginForm'} 在您的 settings.py 文件中,其中 YourLoginForm 继承原始类。

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

这篇关于覆盖使用ACCOUNT_FORMS的Django allauth登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆