07-05-01) django經過如下中間件提供對認證的支持 'django.contrib.sessions.middleware.SessionMiddleware' 'django.contrib.auth.middleware.AuthenticationMiddleware'python
應用模塊爲 'django.contrib.auth' 'django.contrib.sessions' 'django.contrib.admin'django
07-05-02) django登陸與註銷login/logout login:session
<!-- lang: python --> from django.contrib.auth import authenticate, login def my_view(request): username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) # Redirect to a success page. else: # Return a 'disabled account' error message else: # Return an 'invalid login' error message.
logout:函數
<!-- lang: python --> from django.contrib.auth import logout def logout_view(request): logout(request) # Redirect to a success page.
###若是是非登陸用戶使用logout註銷時,此函數不會提示任何錯誤或引起異常 ###調用logout時,當前登陸中的session會話數據(sessionid)會自動清除code
07-05-03) 詳細瞭解django.contrib.auth下的login/logout的API中間件