【问题标题】:Django-crispy-forms AttributeError when using built in AuthenticationForm使用内置 AuthenticationForm 时的 Django-crispy-forms AttributeError
【发布时间】:2013-04-05 23:04:19
【问题描述】:

我正在尝试使用 django-crispy-forms 在 django 中显示带有登录视图的内置 AuthenticationForm。我在继承 AuthenticationForm 时遇到问题 - 我收到 AttributeError。错误显示'WSGIrequest' object has no attribute '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)

我认为此错误与通过 get 从重定向调用的登录视图有关(我使用的是 @login_required 装饰器)。有人对如何使用 django-crispy-forms 子类化内置表单并避免此错误有任何想法吗?

【问题讨论】:

    标签: python django django-crispy-forms


    【解决方案1】:

    您的表单中似乎有错误:

    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

    【讨论】:

    • 我知道这将是一件我忽略的简单事情!感谢您发现...我的头痛现在已经消失了:-)
    • 很高兴它对您有所帮助 :) 顺便说一句,通常最好尽快调用父方法。已经更新了crispy-forms 文档以显示这一点。
    猜你喜欢
    • 2015-02-02
    • 2019-07-31
    • 1970-01-01
    • 2012-06-16
    • 2014-06-22
    • 2016-12-20
    • 2019-06-05
    • 2021-05-29
    • 1970-01-01
    相关资源
    最近更新 更多