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
相关推荐
黄忠8 小时前
01-系统架构设计-LangGraph状态机与多源异构RAG
python
zzzzzz3108 小时前
假如我是掘金管理员,我先给评论区装个'代码审查'系统
python·程序员·机器人
砍材农夫9 小时前
python环境|conda安装和使用(2)
后端·python
程序员龙叔1 天前
编写高质量 Skill 系列 -- 如何设计需求分析与用例生成的 SKILL
自动化测试·软件测试·python·软件测试工程师·接口测试·性能测试·skill·ai测试
大树881 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
用户8356290780511 天前
使用 Python 操作 Word 内容控件
后端·python
小宇宙Zz1 天前
Maven依赖冲突
java·服务器·maven
码云骑士1 天前
32-慢查询排查全流程(下)-索引优化实战与最左前缀原则
python
古城小栈1 天前
Unix 与 Linux 异同小叙
linux·服务器·unix