Django's auth module (user authentication)

Introduction to the auth module

The auth module is an encapsulation of the login authentication method. Before we obtained the user name and password entered by the user, we needed to query the user table for objects that match the user name and password.

With the auth module, it is easy to verify whether the user's login information exists in the database.

In addition, auth also encapsulates the session, which is convenient for us to verify whether the user is logged in

method in auth

First import the module

from django.contrib import auth

1 、authenticate()  

Provides user authentication, that is, to verify whether the user name and password are correct, generally requires two keyword parameters of username and password

If the authentication information is valid, a User object is returned. authenticate() will set an attribute on the User object to identify which authentication backend authenticated the user, and this information is required in the subsequent login process. When we try to log in to a User object that is directly retrieved from the database without authenticate(), an error will be reported! !

user = authenticate(username='someone',password='somepassword')

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324886602&siteId=291194637