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
相关推荐
0+11113 小时前
Linux --DNS与ICMP
linux·运维·服务器
严谨的麻辣烫13 小时前
静态代理 IP 的出口一致性如何验证?Python 请求检测与异常排查
开发语言·python·http·网络安全
我命由我1234513 小时前
5-1.Python 编码进阶 - 函数进阶(参数、返回值、递归、函数作为参数传递、lambda 匿名函数)
java·服务器·数据库·后端·python·java-ee·python3.11
FITA阿泽要努力13 小时前
Fork Agent:共享上下文的艺术
开发语言·python·agent
DeeGLMath14 小时前
optimtool最新版研究梯度优化
python·optimtool·梯度优化
坐吃山猪14 小时前
Python-Hermite基函数
开发语言·python
服务器硬件项目经理14 小时前
全系列目录导航
服务器·gru·硬件
库玛西14 小时前
计算机网络第三章:数据链路层
服务器·网络·计算机网络·考研·算法·面试
空~自由14 小时前
Python 正则 . * ? .* .*? (.*?) 通俗讲解
开发语言·python
Web3&Basketball14 小时前
DeepSeek V4.1 Flash 实测:Token 砍半、Search API 跑通
数据库·人工智能·python·大模型·api调用·deepseek·推理优化