python – 使用内置AuthenticationForm时的Django-crispy-forms AttributeError

我正在尝试使用
django-crispy-forms在django中显示带有登录视图的内置AuthenticationForm.我有问题继承AuthenticationForm – 我得到一个AttributeError.错误说’WSGIrequest’对象没有属性’get’.这是我的表格:

class LoginForm(AuthenticationForm):
    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_tag = False
        self.helper.layout = Layout(
            Field('username', placeholder="username"),
            Field('password', placeholder="password"),)
        super(AuthenticationForm, self).__init__(*args, **kwargs)

我认为这个错误与通过重定向获取的登录视图有关(我正在使用@login_required装饰器).有没有人对如何使用django-crispy-forms子类化内置表单有任何想法,并避免此错误?

最佳答案 看起来您的表单中有错误:

class LoginForm(AuthenticationForm):
    def __init__(self, *args, **kwargs):
        super(LoginForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_tag = False
        self.helper.layout = Layout(
            Field('username', placeholder="username"),
            Field('password', placeholder="password"),
        )

你正在调用super,传递父类AuthenticationForm而不是LoginForm.

点赞