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
相关推荐
工程师老罗10 分钟前
Pytorch中的优化器及其用法
人工智能·pytorch·python
豆是浪个14 分钟前
Linux(Centos 7.6)命令详解:top
linux·运维·服务器
2301_8223650318 分钟前
实战:用Python分析某电商销售数据
jvm·数据库·python
luoluoal19 分钟前
基于python的人脸识别的酒店客房入侵检测系统(源码+文档)
python·mysql·django·毕业设计·源码
子午34 分钟前
【2026计算机毕设~AI项目】鸟类识别系统~Python+深度学习+人工智能+图像识别+算法模型
图像处理·人工智能·python·深度学习
流㶡1 小时前
网络爬虫库与robots.txt规则
python·网络爬虫
2301_788756061 小时前
Python在2024年的主要趋势与发展方向
jvm·数据库·python
阿部多瑞 ABU1 小时前
`tredomb`:一个面向「思想临界质量」初始化的 Python 工具
前端·python·ai写作
u0109272711 小时前
Python虚拟环境(venv)完全指南:隔离项目依赖
jvm·数据库·python
m0_686041611 小时前
Python类型提示(Type Hints)详解
jvm·数据库·python