當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。