In introducing this section, we need to refer to the following two modules:
From django.contrib.auth.models import userfrom django.contrib Import auth
First, User table introduction
Where user is the Django framework gives us a table of users to store information such as logged-in users and passwords, when we use this Django authentication system, we must use this table, you can not create another user table, and the contents of the table can not be added directly in the table, because it stores the password is an encrypted string. Where the username and Password fields are fields that must be deposited into the content. There are two ways to add data to the user table:
1. Method One:
Add the Superuser information by using the following command: Python manage.py createsuperuser, and then follow the steps to set up the user information, where the username and password fields are the fields that must be deposited into the content.
2, Mode two:
Add user information via Create_user () or Create_superuser () (same as above to create a superuser effect), as follows for an example of an application that registers a user function:
def register (Request): if request.method== "POST": username=request. Post.get ("username") userpswd1=request. Post.get ("Userpswd1") userpswd2=request. Post.get ("Userpswd2") if Userpswd1==userpswd2: User.objects.create_user (username=username,password= USERPSWD1) #username和password为User表的关键字段 return Redirect ("/login/")
Second, author module method
1, Authenticate ()
Provide user authentication, that is, verify the user name and password is correct, generally need to username password two keyword parameters, if the authentication information valid, will return a user object. For specific use, see the following login function instance:
DEF login (Request): if request.method== "POST": username=request. Post.get ("username") userpswd=request. Post.get ("userpswd") user=auth.authenticate (username=username,password=userpswd) if User: Auth.login (Request,user) # equivalent to request.session["user"]=user.username return Redirect ("/booklist/") return render (Request, "login.html")
2. Login (HttpRequest, user)
The function accepts a HttpRequest object, and an authenticated user object, which uses the Django session framework to attach information such as session ID to a certified person. The session usage is set in the above login instance.
3. Logout (Request)
The function accepts a HttpRequest object with no return value. When the function is called, the session information for the current request is cleared. Even if the user is not logged in, using this function will not error. Log off the session instance of the currently logged on user as follows:
def login_out (Request): auth.logout (Request) # Clears the session of the current logged-in user, equivalent to Request.session.flush () return Redirect ("/login/")
Third, the User object method
1, is_authenticated ()
The return value is a bool value, mainly used to determine whether the user object to complete login verification, such as to require users to log in to access certain URLs, or jump to the login page to complete the login. See the following validation example before the page function is accessed:
def booklist (Request): user=request.user #显示当前登录用户对象 if not user.is_authenticated (): return Redirect ("/login/") Book_obj=book.objects.all () return render (Request, "booklist.html", {"Book_obj": Book_ OBJ})
2, Set_password ()
Use Set_password () to modify the password, as shown in the following change Password function example:
def modify (Request): if request.method== "POST": username=request. Post.get ("username") pre_userpswd=request. Post.get ("pre_userpswd") new_userpswd=request. Post.get ("new_userpswd") user=auth.authenticate (username=username,password=pre_userpswd) if User: User.set_password (NEW_USERPSWD) user.save () return redirect ("/login/")
Iv. Common Django Commands
The commands commonly used in the Django framework are summarized below, note that the following commands are executed at the terminal:
# view Django version python-m Django--version# Create project named Mysitedjango-admin startproject mysite# Django Project environment Terminal Python manage.py shell# Create the application and make sure that manage.py is the same directory as Python manage.py startapp app01# start Djangopython manage.py Runserverpython manage.py runs erver 8080python manage.py runserver 0.0.0.0:8000# Database Migration python manage.py makemigrationspython manage.py migrate# Synchronize database python manage.py syncdb# empty database (keep empty table) Python manage.py flush# Authentication user table Create Superuser python manage.py createsuperuser# Modify user password Python manage.py changepassword username
Python Day77 Auth and User