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
相关推荐
郝学胜-神的一滴9 分钟前
系统设计 014:缓存深度实战:如何用 Cache 优雅优化数据库读写?
java·数据库·python·缓存·oracle·php·软件构建
Cloud_Shy61813 分钟前
解读《Effective Python 3rd Edition》:从练气到老魔(第三章 Item 17 - 20)
开发语言·笔记·python
七仔啊14 分钟前
windows server服务器验机流程
运维·服务器·windows
Cx330❀18 分钟前
【Linux网络】打破“一问一答”局限:从零构建全双工多线程UDP群聊系统
linux·运维·服务器·网络·网络协议·udp
码农小旋风22 分钟前
Codex中文网 | Codex CLI 中文指南
运维·服务器·ide·人工智能·chatgpt·claude
ZHW_AI课题组24 分钟前
使用Stable Diffusion v1.5文本引导与无分类器引导(CFG)算法实现条件生成图片
人工智能·python·算法·机器学习·stable diffusion
通街市密人有25 分钟前
解决实验室服务器无法连网问题
服务器
tedcloud12325 分钟前
Dolt部署教程:打造可追踪数据变更的数据库环境
服务器·数据库·人工智能·学习·自动化·powerpoint
盼小辉丶26 分钟前
OpenCV-Python实战(25)——基于深度传感器与凸性分析打造实时手势识别系统
人工智能·python·opencv·计算机视觉
学且思27 分钟前
SSE 是 HTTP/2 的服务器推送?
服务器·网络协议·http