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
相关推荐
典学长编程6 小时前
LangGraph 快速入门:从 StateGraph 到 ReAct Agent
python·有向图·无向图·langgraph·react agent·stategraph
Summer-Bright7 小时前
企业落地智能体,供应链签名还是空白:1862 个 MCP 服务器无认证裸奔
服务器·人工智能·github·mcp
用户019027581619 小时前
AlphaFeed 查不到代码返回空?非法/缺后缀 symbol 的行为与处理
python
isfox9 小时前
Python 单元测试:别再用 print 调试了,写个测试一劳永逸
python
用户298698530149 小时前
在 Python 中把 Word 文档转换为 JSON 的三种实现方式
python·json·api
用户03321266636710 小时前
使用 Python 删除 Excel 中的空白行或空白列
python·excel
databook10 小时前
时间序列特征,哪些当时就能算出来
python·数据分析
kevin_org10 小时前
LangChain入门学习(四)
python·学习·langchain·fastapi
特立独行的猫A19 小时前
别再用 nohup 了:让应用在后台常驻的正确姿势与 systemd 服务详解
java·后端·python
吃我一窝jio21 小时前
Python与Go线程模型深度对比
java·开发语言·python