1. Create a Super User
- Enter Python manage. py createsuperuser in the command line.
2. Authenticate the user name and password)
Import the auth module first: from Django. contrib import auth
Syntax: OBJ = Auth. Authenticate (request, username, password)
An object is returned for successful authentication. If the authentication fails, the value is none.
Authenticate provides the user authentication function, that is, to verify whether the user name and password are correct. Generally, the username and passeord parameters are required. If the authentication succeeds (the user name is valid correctly), a user object is returned.
Authenticate () sets an attribute for the user () object to identify the system that has been authenticated by the backend, and this information is required during subsequent login (see figure)
Related code:
From Django. shortcuts import render, httpresponse, redirectfrom Django. contrib import authfrom about_auth.froms import regformdef login (request): # If the request comes in, submit the data if request. method = "Post": # Get user input value user = request. post. get ("user") Pwd = request. post. get ("PWD") #1. to verify whether the user name and password are correct, use the username and password keyword OBJ = Auth. authenticate (username = user, password = PWD) # If the authentication succeeds, a user object is returned. If the authentication fails, a none print (OBJ, type (OBJ) is returned. #2, determine the property of OBJ #2.1. If the object obj is a user that saves session data, no return value is returned if OBJ: Data = Auth. login (request, OBJ) print (data) # obtain the user input address next = request. get. get ("Next") # judge the address entered by the user. If it is login, next is none, otherwise it is not if next: Redirect (next) else: redirect ("/login") # The first incoming request is a GET request. Return render (request, "login.html") to the user's logon page ")
3. Save the logon status and record it to the session (LOGIN (request, user ))
This function receives an httprequest object and an authenticated user object. This function implements a user login function, which generates session data for the user at the backend.
Related code:
From Django. shortcuts import render, httpresponse, redirectfrom Django. contrib import authfrom about_auth.froms import regformdef login (request): # If the request comes in, submit the data if request. method = "Post": # Get user input value user = request. post. get ("user") Pwd = request. post. get ("PWD") #1. to verify whether the user name and password are correct, use the username and password keyword OBJ = Auth. authenticate (username = user, password = PWD) # If the authentication succeeds, a user object is returned. If the authentication fails, a none print (OBJ, type (OBJ) is returned. #2, determine the property of OBJ #2.1. If the object obj is a user that saves session data, no return value is returned if OBJ: Data = Auth. login (request, OBJ) print (data) # obtain the user input address next = request. get. get ("Next") # judge the address entered by the user. If it is login, next is none, otherwise it is not if next: Redirect (next) else: redirect ("/login") # The first incoming request is a GET request. Return render (request, "login.html") to the user's logon page ")
4. log out (request ))
This function accepts an httpresponse object and has no returned value. When this function is called, the session information of the current request is cleared. Even if the user is not logged on, no error is returned when using this function:
Related code:
Def logout (request): # Delete session data Auth. logout (request) return redirect ("/login /")
5. Determine the logon status: used to determine whether the current request has been authenticated (is_authenticated ())
Def index (request): # judge the logon status and return a Boolean value. If the request is successful, true is returned. For, false print (request. user. is_authenticated () return render (request, "index.html ")
6. Create a user:
- Table name. Objects. Create (). Create a common user with a plaintext password.
- Table name. obiects. Create _ table name. Create a common user with a ciphertext Password
- Table name. Objects. create_super table name creates a superuser with a ciphertext Password
The Code is as follows:
From Django. shortcuts import render, httpresponse, redirectfrom Django. contrib import authfrom about_auth.froms import regformfrom Django. contrib. auth. models import userfrom about_auth.froms import regformdef register (request): form_obj = regform () # The second request is a POST request to submit data if request. method = "Post": # instantiate a regform object to verify whether the submitted data is valid form_obj = regform (request. post) # If the verification is passed if form_obj.is_valid (): # to obtain valid data, it is a dictionary data_dict = form_obj.cleaned_data # To delete the password entered again, then store the data in the database data_dict.pop ("re_password") # update the data to the database #1. register the super user and the password is ciphertext # user. objects. create_superuser (email = "", ** data_dict) #2. register an ordinary user and the password is ciphertext # user. objects. create_user (** data_dict) #3. register an ordinary user with a plaintext password. objects. create (** data_dict) return httpresponse ("registered") return render (request, "register.html", {"form_obj": form_obj })
7. Password-related
Verify old password: request. Table name. check_password ("root1234 ")
Set Password: request. Table name. set_password ("admin1234 ")
Request. Table name. Save ()
Code Demonstration:
Def index (request): # judge the logon status and return a Boolean value. If the request is successful, true is returned. For, false print (request. user. is_authenticated () # change the password, first obtain whether the old password entered by the user is consistent with the original password stored in the database. # When the password is consistent, store the new password entered by the user in the database # Put the password in the database and verify the password for the number of users on a daily basis. If the password is consistent with the if request. user. check_password ("root1234"): # update the password entered by the user to the database for request. user. set_password ("admin1234") # Remember to save () to change the operations on the database in the ORM to the database operation request. user. save () return render (request, "index.html ")
8. Extend the default auth_user table
We can inherit the built-in abstracuser class to define a model class,
From Django. contrib. auth. models import abstractuserclass userinfo (abstractuser): "User Info table" nid = models. autofield (primary_key = true) Phone = models. charfield (max_length = 11, null = true, unique = true) def _ STR _ (Self): return self. username
Note: After expanding the table, you must add
# Reference the user table that comes with Django. You need to set auth_user_model = "app name. userinfo" when using inheritance"
In settings. py, tell Django that I am using my new userinfo table for user authentication. (The table name has changed)
Auth module in Django