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
相关推荐
zzzyyy5384 分钟前
Linux之缓冲区
linux·运维·服务器
hwscom9 分钟前
Linux服务器如何进行安全加固,防止黑客攻击(Windows也适用)
linux·服务器·安全
liann11927 分钟前
3.4_Linux 应急响应排查速查命令表
linux·运维·服务器·安全·网络安全·系统安全
孪生质数-28 分钟前
Linux高危漏洞通报Copy Fail - CVE-2026-31431
linux·运维·服务器·ubuntu·网络安全·debian·cve-2026-31431
vortex530 分钟前
Villain:新一代轻量级 C2 框架完整使用指南
python·网络安全·kali·c2
测试员周周32 分钟前
【AI测试系统】第5篇:AI 编码工具抛硬币?我们用 LangGraph 做了个“确定性+AI”的测试系统(附自愈架构)
人工智能·python·功能测试·测试工具·架构·langchain·单元测试
IMPYLH33 分钟前
Linux 的 tee 命令
linux·运维·服务器·bash
Levin__NLP_CV_AIGC34 分钟前
py文件中文件复制方法
开发语言·python
庚昀◟39 分钟前
腾讯云 CVM + Docker + Jenkins + GitLab CI/CD 全流程指南(python、flask实现简单计算器)
python·ci/cd·docker·flask·jenkins
H_unique40 分钟前
LangChain:创建工具Ⅰ
python·langchain