007:Django Cookie Session

本章知识点

  1. Cookie和session的认识
  2. Cookie与session
  3. cookie、session装饰器
    在这里插入图片描述
    1、Cookie和session的认识
    Cookie(曲奇):用户识别。
    西游记:
    李世民 通关文牒 唐僧 通关文牒 女儿国 通关文牒 比丘国
    http请求实际是无状态
    用户向服务器发起请求,服务器下发cookie到本地,下次请求,用户携带cookie进行请求,
    Cookie解决用户身份问题
    但是cookie不安全

Session是为了解决cookie的安全问题
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2、Cookie与session
Cookie下发
cookie是从响应发的。
Session:在计算机中,尤其是在网络应用中,称为"会话控制"。Session 对象存储特定用户会话所需的属性及配置信息。这样,当用户在应用程序的 Web 页之间跳转时,存储在 Session 对象中的变量将不会丢失,而是在整个用户会话中一直存在下去。
在这里插入图片描述
response = HttpResponseRedirect("/index/")
response.set_cookie() #设置cookie
#key 键
#value = ''值
#max_age = None,寿命 秒
#expires = None, 过期时间,和寿命时候冲突的
#path = ‘/’, cookie起作用的路径,整个网站 /student/
#domain = None, cookie起作用的域名 www.baidu.com
#secure = False, True用https协议传输cookie
#httponly = False, 只用http协议传输,通常情况下,js也可以拿到cookie,但是配置httponly为True,就http可以拿到
请求登录,
第一次请求登录页面 get
在这里插入图片描述
第二次请求登录接口 post Form表单
在这里插入图片描述
Cookie 识别
在这里插入图片描述
3、cookie、session装饰器
#装饰器:
#共性
#个性
#给个性的函数添加共性的功能
def money(fun):
def inner():
print(“xxx 用心制造快乐”)
fun()
return inner
@money
def python_intruce():
print(“we are python”)
@money
def UI_intruce():
print(“we are ui”)
@money
def java_intruce():
print(“we are java”)
python_intruce()
在视图views里添加:
在这里插入图片描述
def loginValid(fun):
def inner(request,*args,**kwargs):
cookie = request.COOKIES
email = cookie.get(“email”)
if email:
return fun(request,*args,**kwargs)
else:
return HttpResponseRedirect("/login/")
return inner

Csdn登录逻辑
1、在请求登录页面的时候,就开始下发cookie
2、在请求登录接口的时候,会校验第一步的cookie
3、Form表单当中添加隐藏域

本章总结
Cookie和session的认识
Cookie与session
cookie、session装饰器

猜你喜欢

转载自blog.csdn.net/weixin_43582101/article/details/86013733