Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在views.py中调用django的自定义编写AuthenticationBackend的authenticate()方法?_Python_Django_Django Authentication_Custom Authentication - Fatal编程技术网

Python 如何在views.py中调用django的自定义编写AuthenticationBackend的authenticate()方法?

Python 如何在views.py中调用django的自定义编写AuthenticationBackend的authenticate()方法?,python,django,django-authentication,custom-authentication,Python,Django,Django Authentication,Custom Authentication,我正在从事一个Django项目,在该项目中,我定义了一个自定义用户模型,我需要为该模型编写自定义身份验证方法,方法如下所示,但我在视图中调用它时遇到问题。py请帮我查看以下代码 我已将自定义后端定义如下 我的自定义身份验证后端 从django.contrib.auth.backends导入BaseBackend 从。模型导入用户 从IntellerMatrix.CommonUtilities.constants导入常量 类AuthenticationBackend(BaseBackend): "

我正在从事一个Django项目,在该项目中,我定义了一个自定义用户模型,我需要为该模型编写自定义身份验证方法,方法如下所示,但我在视图中调用它时遇到问题。py请帮我查看以下代码
我已将自定义后端定义如下
我的自定义身份验证后端

从django.contrib.auth.backends导入BaseBackend
从。模型导入用户
从IntellerMatrix.CommonUtilities.constants导入常量
类AuthenticationBackend(BaseBackend):
"""
认证后端
:管理用户的身份验证过程
"""
def身份验证(self,email=None,password=None):
user=user.objects.get(email=email)
如果用户不是None和user。请检查\u密码(密码):
如果user.is_active==Constants.YES:
返回用户
其他:
返回“用户未激活”
其他:
一无所获
def get_用户(自身,用户id):
尝试:
返回User.objects.get(pk=User\u id)
除User.DoesNotExist外:
一无所获
设置.py

AUTHENTICATION\u BACKENDS=['Modules.users.AUTHENTICATION.AuthenticationBackend',
'django.contrib.auth.backends.ModelBackend',]
视图.py

def登录(请求):
电子邮件ialihaider75@gmail.com'
密码='ali'
user=#如何在此处调用自定义身份验证后端的身份验证方法
如果用户为无:
返回HttpResponse(无效)
其他:
返回HttpResponse(用户)

与其直接调用后端类的
authenticate()
方法,不如使用函数:

user = authenticate(email=email, password=password)
这更一般,使您的代码更灵活。因为您可能有不同的身份验证后端,它们接受不同的参数,例如令牌。因此,您不必导入所有后端并尝试每个后端,只需将所需参数传递给
authenticate
函数,这将自动调用每个auth后端。

您可以调用

使用
authenticate()
验证一组凭据。它将凭据作为关键字参数,
username
password
作为默认情况,根据每个身份验证后端检查它们,如果凭据对后端有效,则返回一个
User
对象。因此:


此外,这不会登录您的使用,这只是检查凭据是否有效。因此,如果您想登录用户,您仍然需要调用。

authenticate()方法来自哪个库,我的意思是我必须导入您上面提到的要调用的authenticate()。@IPFlashorAli:如顶行所述:
来自django.contrib.auth导入authenticate
确定,我已经调用了它,但是我的身份验证方法失败了。我想我的自定义方法没有调用它,而Django总是调用它的默认身份验证()method@IPFlashorAli:正如文档指定的:它将迭代后端并调用这些。但是,您忘记添加
请求
参数,这可能是它失败的原因之一。我建议仔细阅读文档,并尊重这些条件,否则它确实可能不起作用。请注意,您需要将参数(
电子邮件
密码
)作为命名参数传递。
from django.contrib.auth import authenticate

def login(request):
    email = 'ialihaider75@gmail.com'
    password = 'ali'
    user = authenticate(request, email=email, password=password)

    if user is None:
        return HttpResponse('<p>Not Valid</p>')
    else:
        return HttpResponse(user)
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 User.DoesNotExist:
            return None
        if user is not None and user.check_password(password):
            if user.is_active == Constants.YES:
                return user
        return None