Django之Cookie

Django之Cookie

目录

介绍

当我们上网使用社交媒体或者购物时,浏览器需要通过一种方式来记住我们。想象一下你进入一家商店,店员会将一张带有身份信息的会员卡片递给你,并让你每次进店时都将卡片带在身上,这张小卡片就是Cookie

Cookie是由服务器发送到用户的浏览器中的小型文本文件,它存储在用户的计算机上,方便用户每次访问同一个浏览器页面时能够让服务器对其识别

Django操作Cookie

三板斧中的redirect HttpResponse render对象都可以操作Cookie

设置Cookie

python 复制代码
def test(request):
    # response设置为响应对象
    response = HttpResponse()
    # 为该对象绑定Cookie并赋值
    response.set_cookie('name','Paul')
    # 将有Cookie的响应对象返回给web端
    return response
浏览器查看Cookie

获取Cookie

python 复制代码
def test2(request):
    my_cookie = request.COOKIES.get('name')
    print(my_cookie)
    return HttpResponse('ok')
python 复制代码
# 输出:Paul

设置超时Cookie

python 复制代码
# 设置超时Cookie
def test(request):
    response = HttpResponse()
    response.set_cookie('name', 'Paul',max_age=5)
    return response

# 间隔五秒获取两次Cookie
def test2(request):
    my_cookie = request.COOKIES.get('name')
    print(my_cookie)
    time.sleep(5)
    print(my_cookie)
    return HttpResponse('ok')
python 复制代码
# 输出:Paul
# 	   None

注销Cookie

python 复制代码
def test2(request):
    # 将浏览器重定向的同时注销Cookie
    response = redirect('home')
    response.delete_cookie('name')
    return response

模拟登录验证

python 复制代码
def test(request):
    form_obj = UserForm()
    user = request.POST.get('user')
    pwd = request.POST.get('pwd')
    if user =='123123' and pwd == '123123':
        response = HttpResponse('登录成功')
        response.set_cookie('user','123123')
        return response
    return render(request,'app01/register.html',locals())


def test2(request):
    response = redirect('home')
    if request.COOKIES.get('user'):
        return response
    return HttpResponse('请先登录')

登录验证装饰器

python 复制代码
def check_cookie(func):
    def inner(request, *args, **kwargs):
        if request.COOKIES.get('user'):
            res = func(request, *args, **kwargs)
            return res
        else:
            return HttpResponse('请先登录')
    return inner

@check_cookie
def test2(request):
    response = redirect('home')
    return render(request, 'app01/home.html')

登录验证装饰器-升级版

python 复制代码
def login(request):
    form_obj = UserForm()
    user = request.POST.get('user')
    pwd = request.POST.get('pwd')
    if user == '123123' and pwd == '123123':
    	# 获取Cookie后直接重定向到url中next的路由,如果没有则返回home
        response = redirect(request.GET.get('next', 'home'))
        response.set_cookie('user', '123123')
        return response
    return render(request, 'app01/register.html', locals())


def check_cookie(func):
    def inner(request, *args, **kwargs):
        target_url = request.get_full_path()
        if request.COOKIES.get('user') == '123123':
            res = func(request, *args, **kwargs)
            return res
        else:
            return redirect(f'/app01/test/?next={target_url}')

    return inner


@check_cookie
def home(request):
    return HttpResponse('这里是home界面')
相关推荐
zzywxc7872 分钟前
AI赋能千行百业:金融、医疗、教育、制造业的落地实践与未来展望
java·人工智能·python·microsoft·金融·golang·prompt
星楠_0019 分钟前
logits和softmax分布
人工智能·python·深度学习
一只学java的小汉堡10 分钟前
Spring Boot 配置详解:从引导器到注解实战(初学者指南)
java·spring boot·后端
__XYZ10 分钟前
Vala编程语言高级特性-弱引用和所有权
c语言·开发语言·后端·c#
IT_陈寒15 分钟前
Python开发者必坑指南:3个看似聪明实则致命的‘优化’让我损失了50%性能
前端·人工智能·后端
Ivanqhz35 分钟前
Rust的错误处理
开发语言·后端·rust
easyboot39 分钟前
python的print加入颜色显示
开发语言·python
aopstudio2 小时前
llms.txt:为大模型打造的“网站说明书”
人工智能·python·llm·开发者工具
java1234_小锋3 小时前
[免费]基于Python的Flask+Vue进销存仓库管理系统【论文+源码+SQL脚本】
后端·python·flask
AI视觉网奇6 小时前
rknn yolo11 推理
前端·人工智能·python