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
相关推荐
小北方城市网5 分钟前
GEO 智变新篇:质效双升 + 责任共生,打造 AI 时代本地商业长效增长引擎
大数据·人工智能·python·数据库架构
yueguangni9 分钟前
centos7虚拟机nat模式连接不上xshell方法分享
linux·运维·服务器
Hello_wshuo19 分钟前
锅炉温控系统优化
linux·python·物联网
weixin_4707403625 分钟前
python生成环境部署
开发语言·python
piaopiaolanghua26 分钟前
Python中的SGP4轨道预报库
python·sgp4
Eiceblue26 分钟前
Python 实现 CSV 转 TXT 格式 (单文件 + 批量处理)
开发语言·python·visual studio code
Iridescent112127 分钟前
Iridescent:Day49
python
阿巴~阿巴~41 分钟前
TCP性能优化秘籍:延迟应答、捎带确认与粘包破解之道
运维·服务器·网络·网络协议·udp·tcp
HuaYi_Sir41 分钟前
i.MX6ULL移植uboot Linux buildroot(二)
linux·运维·服务器
曲幽43 分钟前
从安装到上线:一份 Nginx 实战指南,让你的 Web 应用稳建安全
python·nginx·flask·fastapi·web·gunicorn·uvicorn