如何在views.py中调用django自定义编写的AuthenticationBackend的authenticate()方法?
How to call the authenticate() method of Custom written AuthenticationBackend of django in views.py?
我正在开发一个 Django 项目,在该项目中我定义了一个自定义用户模型,我需要为其编写自定义身份验证方法,方法是按照我编写的文档进行操作但我在调用时遇到了问题它在 views.py 请查看以下代码
帮助我
我已将我的自定义后端定义如下
我的自定义身份验证后端
from django.contrib.auth.backends import BaseBackend
from .models import User
from IntellerMatrix.CommonUtilities.constants import Constants
class AuthenticationBackend(BaseBackend):
"""
Authentication Backend
:To manage the authentication process of user
"""
def authenticate(self, email=None, password=None):
user = User.objects.get(email=email)
if user is not None and user.check_password(password):
if user.is_active == Constants.YES:
return user
else:
return "User is not activated"
else:
return None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
settings.py
AUTHENTICATION_BACKENDS = ['Modules.users.authentication.AuthenticationBackend',
'django.contrib.auth.backends.ModelBackend', ]
Views.py
def login(request):
email = 'ialihaider75@gmail.com'
password = 'ali'
user = # how to call here that custom authentication backend's authenticate method
if user is None:
return HttpResponse("<p>Not Valid</p>")
else:
return HttpResponse(user)
与其直接调用后端 class 的 authenticate()
方法,不如使用 authenticate()
函数:
user = authenticate(email=email, password=password)
这更通用并且使您的代码更灵活。由于您可能有不同的身份验证后端,它们接受不同的参数,例如令牌。因此,与其导入所有后端并尝试每个后端,不如将所需的参数传递给 authenticate
函数,这将自动调用每个身份验证后端。
您可以调用 authenticate(..)
function [Django-doc]
Use authenticate()
to verify a set of credentials. It takes credentials as keyword arguments, username
and password
for the default case, checks them against each authentication backend, and returns a User
object if the credentials are valid for a backend. So:
from django.contrib.auth import authenticate
def login(request):
email = 'ialihaider75@gmail.com'
password = 'ali'
user = <b>authenticate(request, email=email, password=password)</b>
if user is None:
return HttpResponse('<p>Not Valid</p>')
else:
return HttpResponse(user)
请注意,您实施的身份验证方法可以不是 return 字符串。正如 documentation on writing an authentication backend 所说:
(…)
Either way, authenticate()
should check the credentials it gets and return a user object that matches those credentials if the credentials are valid. If they’re not valid, it should return None
.
class AuthenticationBackend(BaseBackend):
"""
Authentication Backend
:To manage the authentication process of user
"""
def authenticate(self, request, email=None, password=None):
try:
user = User.objects.get(email=email)
except <b>User.DoesNotExist</b>:
return None
if user is not None and user.check_password(password):
if user.is_active == Constants.YES:
return user
return <b>None</b>
此外,这 不会 登录您的使用,这只是检查凭据是否有效。所以如果你想登录用户,你仍然需要调用login(..)
function [Django-doc]。
我正在开发一个 Django 项目,在该项目中我定义了一个自定义用户模型,我需要为其编写自定义身份验证方法,方法是按照我编写的文档进行操作但我在调用时遇到了问题它在 views.py 请查看以下代码
帮助我
我已将我的自定义后端定义如下
我的自定义身份验证后端
from django.contrib.auth.backends import BaseBackend
from .models import User
from IntellerMatrix.CommonUtilities.constants import Constants
class AuthenticationBackend(BaseBackend):
"""
Authentication Backend
:To manage the authentication process of user
"""
def authenticate(self, email=None, password=None):
user = User.objects.get(email=email)
if user is not None and user.check_password(password):
if user.is_active == Constants.YES:
return user
else:
return "User is not activated"
else:
return None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
settings.py
AUTHENTICATION_BACKENDS = ['Modules.users.authentication.AuthenticationBackend',
'django.contrib.auth.backends.ModelBackend', ]
Views.py
def login(request):
email = 'ialihaider75@gmail.com'
password = 'ali'
user = # how to call here that custom authentication backend's authenticate method
if user is None:
return HttpResponse("<p>Not Valid</p>")
else:
return HttpResponse(user)
与其直接调用后端 class 的 authenticate()
方法,不如使用 authenticate()
函数:
user = authenticate(email=email, password=password)
这更通用并且使您的代码更灵活。由于您可能有不同的身份验证后端,它们接受不同的参数,例如令牌。因此,与其导入所有后端并尝试每个后端,不如将所需的参数传递给 authenticate
函数,这将自动调用每个身份验证后端。
您可以调用 authenticate(..)
function [Django-doc]
Use
authenticate()
to verify a set of credentials. It takes credentials as keyword arguments,username
andpassword
for the default case, checks them against each authentication backend, and returns aUser
object if the credentials are valid for a backend. So:
from django.contrib.auth import authenticate
def login(request):
email = 'ialihaider75@gmail.com'
password = 'ali'
user = <b>authenticate(request, email=email, password=password)</b>
if user is None:
return HttpResponse('<p>Not Valid</p>')
else:
return HttpResponse(user)
请注意,您实施的身份验证方法可以不是 return 字符串。正如 documentation on writing an authentication backend 所说:
(…)
Either way,
authenticate()
should check the credentials it gets and return a user object that matches those credentials if the credentials are valid. If they’re not valid, it should returnNone
.
class AuthenticationBackend(BaseBackend):
"""
Authentication Backend
:To manage the authentication process of user
"""
def authenticate(self, request, email=None, password=None):
try:
user = User.objects.get(email=email)
except <b>User.DoesNotExist</b>:
return None
if user is not None and user.check_password(password):
if user.is_active == Constants.YES:
return user
return <b>None</b>
此外,这 不会 登录您的使用,这只是检查凭据是否有效。所以如果你想登录用户,你仍然需要调用login(..)
function [Django-doc]。