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
相关推荐
归去来 兮15 小时前
Python爬虫爬取数据案例
开发语言·爬虫·python
何以解忧,唯有..15 小时前
Python logging 模块:从入门到精通
python·microsoft·php
绿蚁新亭15 小时前
Markdown Wiki 网页代码块复制至欧拉 Linux Shell 脚本隐形字符故障完整复盘
linux·运维·服务器
quantdash_cc15 小时前
历史数据断层与REST请求慢到崩溃?QuantDash高性能量化数据API终极解决方案
开发语言·python·缓存·php·量化·quantdash
Loongproxy15 小时前
2026年动态IP代理真实可用率怎么横评?爬虫选代理的关键是什么
服务器·网络·网络协议·tcp/ip
熊野君15 小时前
Prompt / RAG / 规则 / Skills —— 定位、协作与实现路线
开发语言·python
mengge.cloud15 小时前
LVS 小白完整教程(Linux虚拟服务器负载均衡)
linux·运维·服务器·负载均衡·lvs
秦渝兴15 小时前
LVS-DR 高可用集群实战:NFS + Web + DNS + Keepalived 全套架构
linux·服务器·网络·lvs
zhangphil15 小时前
Python Web后端框架FastAPI vs Flask
python·ai·llm