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
相关推荐
木子欢儿4 小时前
Debian 13 + vsftpd 搭建FTP服务
linux·服务器·前端·网络·debian
王仔小奶4 小时前
慢SQL治理与性能调优:DBA如何用Shell/Python提升运维效率?
python·sql·dba
用户0332126663674 小时前
使用 Python 快速在 PowerPoint 中生成表格【教程】
python
夜晚回家5 小时前
Python 身份运算符详解 | is 与 == 到底差在哪
python
fengye2071615 小时前
cpython框架结构
python
达~熊迪5 小时前
IP 分配前核查:把在线状态和使用登记一起看
服务器·网络·tcp/ip
qq_谁赞成_谁反对5 小时前
甲方IT的成长之路--SG TCP网络优化工具--20261026
运维·服务器·udp·tcp
用户91294046848425 小时前
大模型返回 JSON 后,先过这道 Python 校验
python
Dmz丶5 小时前
前端转 AI 100 天 Day 32:LLM 客户端工程化——重试、超时、降级、限流
python
曲径海盗5 小时前
【Python 学习第 39 天】for 循环
python·学习笔记·编程入门