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
相关推荐
恒53922 分钟前
Linux文件系统I
linux·运维·服务器
阿成学长_Cain35 分钟前
Linux ipcs 命令超全详解:查看共享内存 / 消息队列 / 信号量,IPC 运维必备
linux·运维·服务器·网络·数据库
nothing&nowhere1 小时前
用 Python 做问卷数据清洗:无效样本检测与处理实战
开发语言·python·数据清洗·数据处理·问卷星·问卷星脚本·刷问卷
青瓦梦滋1 小时前
协议定制/序列化-反序列化(Linux视角)
linux·服务器·网络·c++
花酒锄作田1 小时前
如何发布自己的 Python 库到 PyPI
python
researcher-Jiang2 小时前
Design Patterns——Template Method入门到情景实战
python·设计模式·模板方法模式
飞猪~6 小时前
LangChain python 版本 第一集
开发语言·python·langchain
2601_956319886 小时前
最新AI量化提效,先做可验证的小流程
人工智能·python