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
相关推荐
anew___13 分钟前
《从零手写操作系统 (20):ELF加载器——让OS读懂现代编译器》
java·服务器·前端
全栈练习生19 分钟前
大模型原理之 KV Cache
python·ai
天赐范式31 分钟前
天赐范式第183天:让选择开始生效——育种者方程与选择生效条件
python·数字生命·天赐范式·动态运行时·育种者方程·选择压力·遗传力
皓月盈江1 小时前
Linux系统grep、sed 、awk介绍下,使用方法及区别?
linux·运维·服务器·sed·grep·awk
benchmark_cc1 小时前
批量行情返回后,怎样把请求失败的股票和正常数据分开?
python·数据分析·pandas·量化交易·股票数据·quantdash
宵时待雨1 小时前
MySQL数据库1:数据库基础
服务器·数据库·mysql
云栖梦泽1 小时前
Linux内存管理
linux·运维·服务器
y = xⁿ2 小时前
关于Agent工程落地
前端·人工智能·python
开开心心就好2 小时前
图片白底怎么去掉?抠图工具抠完背景透明
java·服务器·开发语言·pdf·ocr·散列表·启发式算法
小小张说故事2 小时前
pip 安装慢、超时、报错?一份国内源配置 + 9 个高频报错对照表
python