Cookie、Session的具体使用

定义

  保存在客户端浏览器的键值对。

作用场景

  1.登录校验

  2.保存用户的一些偏好信息

Cookie的查询

  查询所有的Cookie信息:

1 request.COOKIES

  完整的Cookie信息就是一个大的字典,具体的Cookie用键值对保存在字典中,因此也可以用下面的这种方式查询单个Cookie信息。

1 request.COOKIES["user"]

   查询原生Cookie信息:

1 request.COOKIES.get("yinzhou")

  查询加盐版Cookie信息:

request.get_signed_cookie("user",salt="woshishuaige",default=None) #获取加盐版cookie信息,如果获取不到就设置为None

要获取的key和salt要和设置的相同才可以成功查询

Cookie的设置

  原生版Cookie设置

1 obj.set_cookie("yinzhou","woshishuaige")

指定key、value即可。

  加盐版Cookie设置

1 set_signed_cookie("user",user,salt="woshishuaige")
 1 def index(request):
 2     # cook = request.COOKIES.get("yinzhou") #获取原生版COOKIE信息
 3     print(request.COOKIES)
 4     print("************",request.COOKIES["user"])
 5     cook_salt = request.get_signed_cookie("user",salt="woshishuaige",default=None) #获取加盐版cookie信息,如果获取不到就设置为None
 6     if cook_salt:
 7         return render(request, "index.html")
 8     else:
 9         return redirect(reverse("login"))
10 
11 
12 def login(request):
13     # 打印所有完整的COOKIES信息
14     print(request.COOKIES)
15     if request.method == "POST":
16         user = request.POST.get("user")
17         pwd = request.POST.get("pwd")
18         if user == "root" and pwd == "admin":
19             obj = redirect(reverse("index"))
20             # obj.set_cookie("yinzhou","woshishuaige") 原生COOKIES
21             obj.set_signed_cookie("user",user,salt="woshishuaige") # 加盐版COOKIE
22             return obj
23     return render(request,'login.html')

Session

定义

  Session是高级版的Cookie,Session可以解决原生Cookie的安全性低的问题。其次Session在服务端,Cookie在客户端,但是Session需要依赖于Cookie的支持才能工作。

需要注意的几点

  Session的步骤:

  1.服务端生成Session_key,自己保留一份,同时给客户端发送一份,Session_key用于唯一标识一个用户,关于用户的数据保存在Session_data中。

  2.当客户端再次访问需要登录验证页面的时候,会携带之前服务端发送的Session_key访问页面,在显示页面之前,服务端会与自己存放在数据库中的Session_key做校验,若一致,则显示页面,不一致则返回登录页面。

  其他:

    1.使用 request.session.delete()方法会删除该用户对应的Session_key在django_session表中的的记录。

    2.一个用户会创建一个Session_key对象。

 1 def check(fun):
 2     def inner(request,*args,**kwargs):
 3         if request.session.get("woshishuaige",None):
 4             # Session通过则显示页面
 5             ret = fun(request,*args,**kwargs)
 6             return ret
 7         else:
 8             #session不通过则返回到登录界面
 9             url = request.get_full_path()
10             return redirect('/login/?from={}'.format(url))
11     return inner
12 
13 
14 @check
15 def index(request):
16     return render(request,"index.html")
17 
18 
19 def login(request):
20     url = request.GET.get("from")
21     user = request.POST.get("user")
22     pwd = request.POST.get("pwd")
23     if request.method == "POST":
24         if user == "root" and pwd == "admin":
25             request.session["woshishuaige"]="liuyinzhou"
26             return redirect(url)
27         else:
28             return redirect(url)
29     return render(request,"login.html")
30 
31 
32 def logout(request):
33     ret = redirect(reverse("login"))
34     ret.delete_cookie("user")
35     return ret
36 
37 
38 @check
39 def home(request):
40     return render(request,"home.html")
原文地址:https://www.cnblogs.com/liuyinzhou/p/8360886.html