django中的cookie与session

获取cookie

request.COOKIE.GET

使用cookie

response.set-cookie

views.py

python 复制代码
from django.http import HttpResponse
from django.shortcuts import render


# Create your views here.
def cookie_test(request):
    r = HttpResponse("hello world")
    r.set_cookie('lan', 'python')
    r.set_cookie('framework','django')
    print(request.COOKIES.get('lan'))
    print(request.COOKIES.get('framework'))
    return r

浏览器中观察cookie值

获取session

request.session.get

使用session

request.session

views.py

python 复制代码
def session_test(request):
    request.session['hello'] = 'world'
    print(request.session.get('hello'))
    return HttpResponse('hello session')

浏览器中查看session ------ 加密显示

用户登录与session
相关推荐
weixin_462446232 分钟前
使用 Python 脚本自动化管理 Docker 容器:启动、修改密码、删除及系统资源监控
python·docker·自动化·系统监控
weixin_4624462314 分钟前
Python 异步下载文件实战:使用 asyncio + aiohttp 实现高并发下载
python·并发下载
bloglin9999915 分钟前
anaconda环境中如何生成requirements
python
筱谙20 分钟前
BES BLE低功耗蓝牙技术实现分析
服务器·网络·网络协议
【赫兹威客】浩哥21 分钟前
【赫兹威客】框架模板-后端bat脚本部署教程
python·django
Cestb0n22 分钟前
某果app 加密校验算法逆向分析
python·算法·逆向安全
薛定谔的猫喵喵25 分钟前
解决 xlrd 2.0+ 版本只支持 xls 格式的问题
python·excel
RisunJan28 分钟前
Linux命令-last(查看用户登录历史)
linux·服务器·网络
2501_9418053140 分钟前
使用Python和Go构建高性能分布式任务调度系统的实践分享
分布式·python·golang
2301_780789661 小时前
2025年UDP洪水攻击防护实战全解析:从T级流量清洗到AI智能防御
服务器·网络·人工智能·网络协议·安全·web安全·udp