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
相关推荐
pride.li19 分钟前
第12章 Python 教程-流程控制:循环结构
linux·服务器·python·算法
ycydynq25 分钟前
django用Bootstrap 模态框 在同一页面实现增删改查
django·bootstrap·jquery
北京中科新远科技26 分钟前
高速光链路故障排查实战:从DOM光功率到FEC与BER
运维·服务器·网络
鬼手点金37 分钟前
模型即 Agent:当模型本身成为产品
开发语言·前端·人工智能·python·机器学习
田里的水稻43 分钟前
PyTorch-Dataset\DataLoader数据加载
人工智能·pytorch·python
卷无止境1 小时前
Python 3.14 自由线程:拆掉 GIL 之后,我们到底得到了什么
后端·python
染指11101 小时前
147.Agent-LangGraph归并函数
开发语言·人工智能·python·langchain·agents
小白学大数据1 小时前
逆向分析短视频平台 a_bogus 参数:从 JavaScript 混淆到 Python 复现
javascript·python·音视频
外收内放1 小时前
数据分析(数据清洗)
python·学习·数据分析