我正在尝试使用 django-crispy-forms 在 django 的登录 View 中显示内置的 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)
我认为此错误与通过从重定向获取调用的登录 View 有关(我正在使用 @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
。
关于python - 使用内置 AuthenticationForm 时的 Django-crispy-forms AttributeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15845023/