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
相关推荐
三宝盈科炜尊3 分钟前
服务器电源保护方案的革新:以智能PTC替代传统NTC的可行性与实践
运维·服务器·能源·开关电源·热敏电阻·ptc热敏电阻
wusam3 分钟前
计算机网络传输层应用层综合实验4:架设ftp网站
服务器·网络·计算机网络·应用层服务
Misnice4 分钟前
使用 SQLAlchemy 连接数据库
数据库·python·mysql·fastapi
龙腾AI白云5 分钟前
【循环神经网络讲解(3)】
python·深度学习
友莘居士7 分钟前
HTTP正向代理 vs TCP反向代理:概念与使用场景详解
运维·服务器·nginx·反向代理
月亮!9 分钟前
当技术中立性遇上算法偏见:软件测试者的伦理启示
网络·人工智能·python·测试工具·算法·安全·开源
曲幽10 分钟前
Flask核心技能:从零上手视图函数
python·json·app·web·get·post·request·response
秦jh_13 分钟前
【Qt】信号与槽
服务器·开发语言·数据库·qt
晞微16 分钟前
PyTorch 实现 BP 神经网络:从函数拟合到分类任务
pytorch·python·神经网络·分类
薛不痒18 分钟前
机器学习之Python中的numpy库,pandas库
开发语言·python