未调用django自定义身份验证后端
我正在使用Django3.1,并试图找出为什么我的自定义身份验证后端没有被调用 在my settings.py文件中,我有:未调用django自定义身份验证后端,django,django-authentication,Django,Django Authentication,我正在使用Django3.1,并试图找出为什么我的自定义身份验证后端没有被调用 在my settings.py文件中,我有: AUTHENTICATION_BACKENDS = ( 'sizzy.backends.EmailBackend', ) from django.core.exceptions import ObjectDoesNotExist from django.contrib.auth.backends import ModelBackend from .models
AUTHENTICATION_BACKENDS = (
'sizzy.backends.EmailBackend',
)
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth.backends import ModelBackend
from .models import User
class EmailBackend(ModelBackend):
def authenticate(self, username=None, password=None):
try:
print("Trying the email backend!")
user = User.objects.get(email=username)
print("Got the user")
if user.check_password(password): # check valid password
return user
print("password didn't work")
except ObjectDoesNotExist:
return None
def get_user(self, user_id):
try:
print("Getting the user of the Email Bkacned")
return User.objects.get(pk=user_id)
except ObjectDoesNotExist:
return None
在我的sizzy.backends.py文件中,我有:
AUTHENTICATION_BACKENDS = (
'sizzy.backends.EmailBackend',
)
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth.backends import ModelBackend
from .models import User
class EmailBackend(ModelBackend):
def authenticate(self, username=None, password=None):
try:
print("Trying the email backend!")
user = User.objects.get(email=username)
print("Got the user")
if user.check_password(password): # check valid password
return user
print("password didn't work")
except ObjectDoesNotExist:
return None
def get_user(self, user_id):
try:
print("Getting the user of the Email Bkacned")
return User.objects.get(pk=user_id)
except ObjectDoesNotExist:
return None
然后打开manage.py shell
并输入:
from django.contrib.auth import authenticate
email = "billypop@a.pop"
password = "password"
user = authenticate(username=email, password=password)
它输出:
Login failed. Credentials:
{'username': 'billypop@a.pop', 'password': '********************'}
我没有看到任何打印的声明。“尝试获取电子邮件后端!”从未打印。我错过什么了吗?我尝试将设置文件更改为指向sizzy.backends.EmailBackendqwertyui
,这只是为了进行健全性检查,结果引发了一个错误,而不是登录失败。因此,我认为后端正在加载,但从未调用过
我还尝试将ModelBackend更改为BaseBackend,结果相同。似乎身份验证(…)
方法的函数签名不正确,应该是
class EmailBackend(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
# do something
class EmailBackend(ModelBackend):
def身份验证(self、request、username=None、password=None、**kwargs):
#做点什么
似乎验证(…)
方法的函数签名不正确,应该是
class EmailBackend(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
# do something
class EmailBackend(ModelBackend):
def身份验证(self、request、username=None、password=None、**kwargs):
#做点什么吧
Arakkal Abu,祝你好运。你又干了!阿拉卡勒·阿布,祝你好运。你又干了!