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
相关推荐
叶子202422几秒前
承认错误才能成长
python
IMPYLH29 分钟前
Linux 的 chmod 命令
linux·运维·服务器
代码探秘者1 小时前
【大模型应用】4.分块之六大策略
java·数据结构·后端·python·spring
艾莉丝努力练剑1 小时前
【MYSQL】MYSQL学习的一大重点:数据库基础
linux·运维·服务器·数据库·c++·学习·mysql
齐齐大魔王1 小时前
虚拟机网络无法连接
linux·网络·c++·python·ubuntu
ycjunhua1 小时前
Notebooklm for windows本地安装使用
python·webstorm
曲辕RPA1 小时前
GEO技术解析:RPA在生成引擎优化中的角色与应用
python·ai·rpa
2401_894241921 小时前
实战:用OpenCV和Python进行人脸识别
jvm·数据库·python
m0_662577971 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
淮北4941 小时前
tmux使用指南
linux·python·html·tmux·md