1.cookies session (not used yet)
The first HTTP access will send some cookies to the browser, and then each visit to the browser will tell the server what its own cookies are.
2.session session (not used yet)
Django's session framework:
(1) The existence of middleware: ' Django.contrib.sessions.middleware.SessionMiddleware '
(2) "django.contrib.sessions" in Installed_apps
After the session frame is activated, you can use Request.session to be a Dictionary object.
3. Users and authentication
(1)Django.contrib.auth ' put in your installed_apps settings
(2) Confirm sessionmiddleware behind middleware_classes settings include Django.contrib.auth.middleware.AuthenticationMiddleware '
After installation, you can use the user, listed as Request.user
Authentication () Validation
Login () logon
Logout () Logout
1 defLogin_ (Request):2 """Login"""3Context = {}4 ifRequest.method = ='POST':5form =LoginForm (request, request.) POST)6 ifform.is_valid ():7user = Form.get_user ()#Get User Instance8 ifUser:9 Login (request, user)Ten ifForm.get_auto_login ():#Set Session One Request.session.set_expiry (None) A - returnHttpresponseredirect ('/') -context['form'] =form the - Else: -form =LoginForm () -context['form'] =form + returnRender (Request,'login.html', context)
(1) Authenticate Authenticated Users (user)
1 fromDjango.contrib.authImportAuthenticate2user = Authenticate (username='John', password='Secret')3 ifUser is notNone:4 #Verify successful return of user object5 ifuser.is_active:6 Print("# is_active Verify whether the user is active")7 Else:8 Print("Inactive words can not log in, will not delete the user, only set the flag bit")9 Else:Ten #validation Failure returns a None One Print("validation Failed")
(2) Default permissions: Generally, the database operation is to delete and change the search (not required)
Reference Link: http://python.usyiyi.cn/django/topics/auth/default.html
Django session, user, and registration