我正在使用 Django 3.1,并试图找出为什么我的自定义身份验证后端没有被调用。
在我的 settings.py 文件中,我有:
AUTHENTICATION_BACKENDS = (
'sizzy.backends.EmailBackend',
)
Run Code Online (Sandbox Code Playgroud)
在我的 sizzy.backends.py 文件中,我有:
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
Run Code Online (Sandbox Code Playgroud)
然后我打开manage.py shell并输入:
from …Run Code Online (Sandbox Code Playgroud)