第62天:django学习(十一)

cookie和session

发展史

一开始,只有一个页面,没有登录功能,大家看到东西都一样。

时代发展,出现了需要登录注册的网站,要有一门技术存储我们的登录信息,于是cookie诞生了。

cookie:

  • 存储形式:k:v键值对

  • 存储位置:客户端

  • 不安全,信息可能会泄露

时代再度发展,需要有一门新的安全的技术,于是有了session。

session:

  • 标识符,来表示我是当前用户加密出来的数据

  • 对敏感信息进行加密处理

  • 存储服务端

  • 标识符配合上你的加密串

  • 把我的标识符+ 字符串全给客户端

  • 客户端存储格式

  • session_id:返回回来的表示符+加密串

django操作cookies

设置cookie
python 复制代码
def login(request, *args, **kwargs):
    if request.method == 'POST':
        username = request.POST.get("username")
        password = request.POST.get("password")
        if username == "kevin" and password == "123":
            obj = HttpResponse("ok")
            obj.set_cookie('sign', 'user')
            return obj
        else:
            return redirect('/login/')
    return render(request, 'login.html')
取值cookie验证
python 复制代码
def home(request, *args, **kwargs):
    sign = request.COOKIES.get('sign')
    if sign and sign == 'user':
        return HttpResponse("这是home页面")
    else:
        return redirect('/login/')
设置过期时间
python 复制代码
obj.set_cookie('sign', 'user', expires=3)
obj.set_cookie('sign', 'user', max_age=3)
删除cookie
python 复制代码
def logout(request, *args, **kwargs):
    obj = redirect('/home/')
    # 设置超时时间 5s 到期
    obj.delete_cookie('sign')
    return obj

django操作session

设置session
python 复制代码
request.session['sign'] = 'user'
取值session
python 复制代码
def login(request, *args, **kwargs):
    # next_url = request.get_full_path()
    # print(next_url) # /login/?next_url=/home/
    if request.method == 'POST':
        username = request.POST.get("username")
        password = request.POST.get("password")
        if username == "dream" and password == "521":
            # next_url = request.GET.get('next_url')
            # print(next_url) # /home/
            request.session['sign'] = 'user'
            obj = redirect('/home/')
            # 设置过期时间
            # obj.set_cookie('sign', 'user', expires=3)
            # obj.set_cookie('sign', 'user', max_age=3)
            return obj
        else:
            return redirect('/login/')
    return render(request, 'login.html')
​
​
def login_auth(func):
    def inner(request, *args, **kwargs):
        # print(request.path_info) #  /home/
        # print(request.get_full_path()) # /home/?username=111
        next_url = request.get_full_path()  # /home/
        # print(next_url)# /home/
        sign = request.session.get('sign')
        # print(sign) # user
        if sign and sign == 'user':
            res = func(request, *args, **kwargs)
            return res
        else:
            return redirect(f'/login/?next_url={next_url}')
​
    return inner
​
​
@login_auth
def home(request, *args, **kwargs):
    return HttpResponse("这是home页面")

注:

  • session基于数据库表才能使用的 - 必须先迁移数据库,生成 django_session 表

  • session只对当次登录有效

  • 主动清除浏览器中本地存在的session

  • 验签发现,没有sessionid就会自动生成新的session

  • `django_sessoin`表中的数据条数取决于浏览器

  • 同一个计算机(IP地址)上同一个浏览器只会有一条数据生效

  • 同一个计算机(IP地址)上多个浏览器会有多个数据生效

  • 当session过期的时候,可能会出现多条数据对应一个浏览器

  • 但是这些数据不会持久化存储,会被定时清理掉,可以手动清除也可以代码清除

  • 目的是为了节省服务器数据库资源

设置session过期时间
python 复制代码
request.session['sign'] = 'user'
request.session.set_expiry(0)
删除cookie
python 复制代码
# 删除session
request.session.delete()
# 把浏览器和数据库里面的session全部清除掉
request.session.flush()

CBV加装饰器的三种方法

python 复制代码
from django.utils.decorators import method_decorator
​
# 方式二:放在类视图上面 (放的装饰器函数,name指定你的视图函数里面的方法)
# @method_decorator(login_auth, name='get')
# @method_decorator(login_auth, name='post')
class UserView(View):
    
    # 方式三 : dispactch 方法加装饰器 : 本视图函数内所有的视图都需要走装饰器
    @method_decorator(login_auth)
    def dispatch(self, request, *args, **kwargs):
        # Try to dispatch to the right method; if a method doesn't exist,
        # defer to the error handler. Also defer to the error handler if the
        # request method isn't on the approved list.
        if request.method.lower() in self.http_method_names:
            handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
        else:
            handler = self.http_method_not_allowed
        return handler(request, *args, **kwargs)
    
    # 方式一:加载视图函数上面
    # @method_decorator(login_auth)
    def get(self, request, *args, **kwargs):
        return HttpResponse("这是home页面")
​
    def post(self):
        ...
相关推荐
张一西3 小时前
docker 容器学习
学习·docker·容器
艾莉丝努力练剑4 小时前
【LeetCode&数据结构】栈的应用——有效的括号问题详解
c语言·开发语言·数据结构·学习·链表
future14126 小时前
游戏开发日志
学习·游戏开发
伍哥的传说6 小时前
React 英语打地鼠游戏——一个寓教于乐的英语学习游戏
学习·react.js·游戏
屁股割了还要学7 小时前
【C语言进阶】结构体练习:通讯录
c语言·开发语言·学习·算法·青少年编程
Jassy1598 小时前
C++二叉搜索树
数据结构·c++·学习
典孝赢麻崩乐急10 小时前
Java学习 ------BIO模型
java·开发语言·学习
空空star10 小时前
matlab学习分析
开发语言·学习·matlab
快乐肚皮13 小时前
ZooKeeper学习专栏(四):单机模式部署与基础操作详解
学习·zookeeper·架构·debian·部署
CIb0la14 小时前
大语言模型训练新标准llms.txt简介
人工智能·学习·程序人生·语言模型·生活