当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python Django authenticate用法及代码示例


本文介绍 django.contrib.auth.authenticate 的用法。

声明

authenticate(request=None, **credentials)

使用 authenticate() 验证一组凭据。它将凭据作为关键字参数,默认情况下为 usernamepassword,针对每个身份验证后端检查它们,如果凭据对后端有效,则返回 User 对象。如果凭证对任何后端都无效,或者后端引发 PermissionDenied ,则返回 None 。例如:

from django.contrib.auth import authenticate
user = authenticate(username='john', password='secret')
if user is not None:
    # A backend authenticated the credentials
else:
    # No backend authenticated the credentials

request 是可选的 HttpRequest ,它通过身份验证后端的 authenticate() 方法传递。

注意

这是验证一组凭据的低级方法;例如,它被 RemoteUserMiddleware 使用。除非您正在编写自己的身份验证系统,否则您可能不会使用它。相反,如果您正在寻找登录用户的方法,请使用 LoginView

相关用法


注:本文由纯净天空筛选整理自djangoproject.com大神的英文原创作品 django.contrib.auth.authenticate。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。