django - django 中的自定义身份验证不起作用
问题描述
我是 django 的新手,我想对用户进行身份验证email
,username
因此password
我编写了一个自定义身份验证,如文档中所示,但它似乎没有被调用,我不知道我该怎么做?
设置.py
AUTHENTICATION_BACKENDS = ('accounts.backend.AuthBackend',)
视图.py
def login(request):
if request.method == 'POST':
username_or_email = request.POST['username']
password = request.POST['password']
user = authenticate(username=username_or_email, password=password)
print(user)
if user is not None:
return reverse('task:home')
else:
messages.error(request, "Username or password is invalid")
return render(request, 'accounts/login.html')
else:
return render(request, 'accounts/login.html')
后端.py
from django.contrib.auth.models import User
from django.db.models import Q
class AuthBackend(object):
supports_object_permissions = True
supports_anonymous_user = False
supports_inactive_user = False
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
def authenticate(self, username, password):
print('inside custom auth')
try:
user = User.objects.get(
Q(username=username) | Q(email=username) )
print(user)
except User.DoesNotExist:
return None
print(user)
if user.check_password(password):
return user
else:
return None
我在课堂上写了这些print
语句,以检查它们是否被调用并在控制台中编写。但是,它们没有被打印出来,并且打印出来的print
声明views.py
None
解决方案
你需要extend
从ModelBackend
django.contrib.auth.backends
from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend
User = get_user_model()
class AuthBackend(ModelBackend):
supports_object_permissions = True
supports_anonymous_user = False
supports_inactive_user = False
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
def authenticate(self, request, username=None, password=None):
print('inside custom auth')
try:
user = User.objects.get(
Q(username=username) | Q(email=username) )
print(user)
except User.DoesNotExist:
return None
print(user)
if user.check_password(password):
return user
else:
return None
并且settings.py
不要忘记添加您的自定义后端身份验证
AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'accounts.backend.AuthBackend'
]
另一种可能的解决方案
从您的代码中,我看到的是您希望您email
应该将其视为User
模型的用户名。您可以轻松修改Django's
AbstructUser
模型,如下所示
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
# your necessary additional fields
USERNAME_FIELD = 'email' # add this line
现在email
字段将被视为 user_name 字段。无需添加自定义authentication-backend
推荐阅读
- python - 为什么 python 元组工厂函数比列表工厂函数慢?
- c++ - c++-使用BST实现策略表报错
- pandas - Pandas:如何获得每组中的前 2 行、中 2 行和后 2 行
- python-2.7 - 我正在使用 python 2.7。面临“END”关键字的问题
- django - 解决方案在 Django 中创建或更新静态页面
- python - 寻找最佳序列图解决方案
- python - 为同一对象的另一个实例更改“自我”?
- c# - 从 *.html 文件中指定 HTML 元素并在其中插入值
- javascript - req.user.roles = 未定义?
- apache-nifi - 删除 nifi 流文件内容的最快方法是什么?