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
相关推荐
陳10307 小时前
Linux:进程间通信 和 简单进程池
linux·运维·服务器
一颗牙牙7 小时前
安装mmcv
开发语言·python·深度学习
大数据魔法师7 小时前
Streamlit(二)- Streamlit 架构与运行机制
python·web
jimy17 小时前
改.bashrc,直观地判断本地repo是否有改动
linux·服务器
m0_470857647 小时前
PHP怎么实现工厂模式_Factory模式编写指南【指南】
jvm·数据库·python
zt1985q7 小时前
本地部署网页监控工具 Webmonitor 并实现外部访问
运维·服务器·网络·网络协议
大数据魔法师7 小时前
Streamlit(三)- Streamlit 多页面应用开发
python·web
我的xiaodoujiao7 小时前
API 接口自动化测试详细图文教程学习系列20--结合Pytest框架使用
python·学习·测试工具·pytest
python在学ing8 小时前
前端-CSS学习笔记
前端·css·python·学习
匆匆那年9678 小时前
远程 Linux 校园网认证操作手册(本地浏览器法)
linux·运维·服务器