- 一、cookie不屬於HTTP協議,能夠填補HTTP協議沒法保存狀態的空缺
- 二、cookie自己最大支持4096字節,自己保存在客戶端,安全性較低
- 三、每一個客戶端cookie分配一個惟一的id,服務器經過cookie的id區分識別用戶
- 四、cookie和session是共通性的,不限制於語言和框架
操做 | 語法 |
---|---|
獲取cookie | request.COOKIES[key] |
設置cookie | response.set_cookie(key.value) |
- session是一次瀏覽器和服務器的交互對話
- session默認在服務端保存15天
- 經過session將cookie信息保存在服務器且有較高的安全性
操做 | 語法 |
---|---|
獲取session | request.session[key] |
設置session | request.session[key] = value |
刪除session | del request.session[key] |
request.session.set_expiry(value)html
- 若是value是整數,session會在對應的秒數後失效
- 若是value是datatime或timedelta,session就會在這個時間後失效
- 若是value是0,用戶關閉瀏覽器session就會失效
- 若是value是None,session會依賴全局session失效策略
Mydiange APP templates index.html login.html apps.py views.py Mydjango setting.py urls.py
建立項目python
django-admin startproject Mydjango cd Mydjango python manage.py startapp APP
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>歡迎{{ name }}進入首頁!!!</h1> <a href="/login">點擊進入登陸界面</a> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登陸頁面</title> <style> *{margin: 0;padding: 0;} </style> </head> <body> <form action="/login/" method="post"> <p>用戶名<input type="text" name="user"></p> <p>密碼<input type="password" name="pwd"></p> <input type="submit"> </form> </body> </html>
from django.apps import AppConfig class AppConfig(AppConfig): name = 'APP'
# coding=utf8 from django.shortcuts import render,redirect def login(request): #登陸頁面 print("COOKIE",request.COOKIES) #顯示Django自帶cookie print("SESSION",request.session) #顯示Django自帶cookie if request.method == "POST": #判斷提交方式 name=request.POST.get("user") #提交的用戶名 pwd=request.POST.get("pwd") #提交的密碼 if name=="admin" and pwd == "123": #提交用戶信息判斷 # ret = redirect("/index/",locals()) #轉到index的變量,未加cookie認證 【非session形式】 # ret.set_cookie("COOKIEID","uqzy2IjOoOf0OXaz") #添加cookie認證信息,可用於判斷用戶是否登陸 【非session形式】 request.session["CertificationID"]=True #自定義認證ID,用於判斷是否登陸 request.session["user"]=name #用於判斷用戶名 request.session.set_expiry(180) #定義session失效時間 return redirect("/index/",locals()) #符合條件轉到index頁面 return render(request,"login.html",locals()) #不符合條件載入登陸界面 def index(request): #首頁 # if request.COOKIES.get("COOKIEID",None): #經過cookie判斷用戶是否登陸 【非session形式】 # name = "admin" 【非session形式】 if request.session.get("CertificationID",None): #經過session的CertificationID判斷是否登陸 name=request.session.get("user",None) #判斷用戶名是否存在 return render(request,"index.html",locals()) #符合條件載入index頁面 else: return redirect("/login/",locals()) #不符合條件轉到login登陸頁面
# coding=utf8 from django.contrib import admin from django.urls import path,re_path from APP import views urlpatterns = [ re_path('^$',views.index), #默認頁 path('index/', views.index), #首頁 path('login/', views.login), #登陸頁 ]
INSTALLED_APPSdjango
'APP', #新增應用名稱
MIDDLEWARE瀏覽器
#'django.middleware.csrf.CsrfViewMiddleware', #測試cookie暫時關閉驗證中間件
TEMPLATES安全
'DIRS': [os.path.join(BASE_DIR, 'APP/templates')], #靜態頁面儲存位置
生成session數據表服務器
python manage.py makemigrations APP python manage.py migrate
運行項目cookie
python manage.py runserver 8000
頁面訪問session
http://127.0.0.1:8000/
- 第一次訪問會自動跳轉到 login登陸頁面
- 在登陸頁面後提交登陸,二次訪問會記錄已登陸直接訪問index頁面
- cookie只適用於同一瀏覽器,跨瀏覽器訪問失效