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
相关推荐
玉鸯5 小时前
Agent Hook:在概率推理之上,为 Agent 叠加确定性控制
python·langchain·agent
weixin_446260855 小时前
HACO:面向动态部署环境的对冲式智能计算可靠多智能体调度框架
后端·python·flask
我的xiaodoujiao5 小时前
API 接口自动化测试详细图文教程学习系列32--Allure测试报告2
python·学习·测试工具·pytest
吳所畏惧6 小时前
宝塔面板Redis密码修改指南:SSH命令修改 vs 面板UI界面修改,哪个更靠谱?
运维·服务器·数据库·redis·缓存·ssh
DFT计算杂谈6 小时前
无 Root 权限在 Tesla K80 零门槛部署 DeepSeek 大模型
linux·服务器·网络·数据库·机器学习
qetfw6 小时前
MXU:Tauri 2 + React 的 MaaFramework 跨平台 GUI 源码
前端·python·react.js·前端框架·开源项目·效率工具
用户8356290780517 小时前
Python 实现 Excel 页面布局与打印设置自动化
后端·python
维天说7 小时前
CLI-Switch 2026年3月版历史设计:Hook、TTY 隔离与 JSON 状态
java·服务器·json
Zhang~Ling7 小时前
从 fopen 到 struct file:从零开始拆解 Linux 文件 I/O
linux·运维·服务器