簡體   English   中英

Django自定義后端導致登錄錯誤

[英]Django custom backend causes error on signin

編寫了自定義django后端,在單擊登錄按鈕后,它將調用身份驗證,並失敗並顯示錯誤:

'dict' object has no attribute 'backend'

我將字符串推送到身份驗證后端

AUTHENTICATION_BACKENDS = [
    'app_auth.auth_backend.AuthBackend'
]

可能出什么問題了?

堆棧跟蹤:

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

驗證功能

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_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

該錯誤表明在身份驗證的第24行中,您正在設置user.backend = 'app_auth.auth_backend.AuthBackend' ,並且該字典沒有該屬性。

您錯誤地訪問了該字段。 如果user是一個字典,並且您想設置一個稱為backend的屬性的值,則第24行應為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM