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
相关推荐
matlabgoodboy5 分钟前
Python代做java代码编写C++大数据R语言Hadoop/spark/flink/C语言
java·大数据·python
清水白石0085 分钟前
《Python 编程全景解析:透视性能瓶颈——从基础测速到线上热点诊断的高阶实战》
开发语言·python
清水白石0086 分钟前
Python 服务优雅停机实战:信号处理、资源收尾与 Kubernetes 滚动发布避坑指南
python·kubernetes·信号处理
gc_22997 分钟前
学习python使用Ultralytics的YOLO26进行目标检测的基本用法
python·目标检测·yolo26
2301_8166512210 分钟前
Django全栈开发入门:构建一个博客系统
jvm·数据库·python
第一程序员16 分钟前
如何在GitHub上找到适合初学者的Python项目
python·github
zzwq.18 分钟前
Python函数进阶:参数类型与返回值详解
python
Chasing Aurora20 分钟前
Python后端开发之旅(五)——DL
开发语言·pytorch·python·深度学习
喵手23 分钟前
Python爬虫实战:手把手带你打造私人前端资产库 - Python 自动化抓取开源 SVG 图标全目录!
爬虫·python·自动化·爬虫实战·零基础python爬虫教学·前端资产库打造·采集svg图标目录
qq_1481153723 分钟前
用Python批量处理Excel和CSV文件
jvm·数据库·python