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
相关推荐
老纪1 分钟前
Angular 表单中基于下拉选择动态启用字段必填校验的完整实现
jvm·数据库·python
weixin_459753942 分钟前
Vue.js响应式shallowReadonly实现对象属性的一层状态保护
jvm·数据库·python
m0_470857642 分钟前
uni-app怎么实现拖拽调整顺序 uni-app可拖动格子布局实现【技巧】
jvm·数据库·python
小明同学012 分钟前
linux———进程间通信
linux·服务器·网络
dFObBIMmai4 分钟前
MySQL如何利用存储过程封装权限_通过DEFINER与INVOKER模式控制
jvm·数据库·python
m0_748554814 分钟前
如何用 stopPropagation 阻止事件冒泡防止触发父级回调
jvm·数据库·python
2301_812539675 分钟前
SQL如何基于窗口函数实现复杂分层 DENSE_RANK应用
jvm·数据库·python
weixin_459753945 分钟前
Go语言怎么做多阶段构建_Go语言Docker多阶段构建教程【完整】
jvm·数据库·python
meng_ser5 分钟前
[NewStarCTF 2023 公开赛道]eazy_crt
开发语言·python
m0_631529826 分钟前
MySQL如何解决锁等待超时异常_捕获MySQL Error 1205错误
jvm·数据库·python