简体   繁体   English

Django自定义后端导致登录错误

[英]Django custom backend causes error on signin

Wrote custom django backend, after clicking signin button it calls authenticate and fails with error: 编写了自定义django后端,在单击登录按钮后,它将调用身份验证,并失败并显示错误:

'dict' object has no attribute 'backend'

I pushed string to authentication backend 我将字符串推送到身份验证后端

AUTHENTICATION_BACKENDS = [
    'app_auth.auth_backend.AuthBackend'
]

What may go wrong? 可能出什么问题了?

Stacktrace: 堆栈跟踪:

Internal Server Error: /sign-in/
Traceback (most recent call last):
  File "C:\django-proj\env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\django-proj\env\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\django-proj\env\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\django-proj\app_auth\views.py", line 12, in sign_in
    user = authenticate(request, username=username, password=password)
  File "C:\django-proj\env\lib\site-packages\django\contrib\auth\__init__.py", line 73, in authenticate
    user = backend.authenticate(request, **credentials)
  File "C:\django-proj\app_auth\auth_backend.py", line 24, in authenticate
    user.backend = 'app_auth.auth_backend.AuthBackend'
AttributeError: 'dict' object has no attribute 'backend'
[21/Feb/2019 22:53:20] "POST /sign-in/ HTTP/1.1" 500 78995
[21/Feb/2019 23:15:25] "GET /sign-in/ HTTP/1.1" 200 1274

Authenticate function 验证功能

from django.conf import settings
from django.contrib.auth.hashers import check_password
from django.contrib.auth.models import User
from services.db.users import get_user, get_user_by_id

class AuthBackend():
    def authenticate(self, request, username=None, password=None):
        user = get_user(username=username)
        return user

    def get_user(self, user_id):
        return get_user_by_id(id=user_id)

get_user and get_user_by_id return stub data get_userget_user_by_id返回存根数据

user = {
    'name': 'user-name',
    'backend': 'app_auth.auth_backend.AuthBackend'
}


def get_user(username):
    return user


def get_user_by_id(id):
    return user

The error says that in line 24 of authenticate you are setting user.backend = 'app_auth.auth_backend.AuthBackend' , and that the dict does not have that attribute. 该错误表明在身份验证的第24行中,您正在设置user.backend = 'app_auth.auth_backend.AuthBackend' ,并且该字典没有该属性。

You are accessing the field incorrectly. 您错误地访问了该字段。 If user is a dictionary and you want to set the value of an attribute called backend then your line 24 should be: 如果user是一个字典,并且您想设置一个称为backend的属性的值,则第24行应为:

user['backend'] = 'app_auth.auth_backend.AuthBackend'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM