- 一、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只适用于同一浏览器,跨浏览器访问失效