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
相关推荐
王闯写bug1 天前
【openEuler】修改ssh端口号
服务器·网络·ssh·openeuler
之歆1 天前
检索增强拦截器-查询转换器&翻译转化器&document后置处理器
运维·服务器
Suchadar1 天前
Linux计划任务进程
linux·运维·服务器
食咗未1 天前
Linux microcom工具的使用
linux·运维·服务器·驱动开发·串口调试
YYYing.1 天前
【计算机网络 | 第四篇】路由与NAT技术
运维·服务器·网络·网络协议·计算机网络
曲幽1 天前
FastAPI缓存提速实战:手把手教你用Redis为接口注入“记忆”
redis·python·cache·fastapi·web·asyncio
Xの哲學1 天前
Linux Worklet 深入剖析: 轻量级延迟执行机制
linux·服务器·网络·数据结构·算法
no24544101 天前
RAGFlow 全面接入 MinerU 2.0,支持 pipeline、vlm-transformers、vlm-sglang 三种模式,解析精度大幅度up
java·大数据·人工智能·python·ai·sglang
Hello.Reader1 天前
CSV Format Flink / PyFlink 读写 CSV 的正确姿势(含 Schema 高级配置)
大数据·python·flink
宴之敖者、1 天前
Linux——初始Linux系统
linux·运维·服务器