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 小时前
什么是ZCode for GLM-5.2?
开发语言·人工智能·python·科技·算法·ai编程·ai写作
心运软件8 小时前
Python实战:中国大学排行榜数据采集与可视化大屏
后端·python
长不胖的路人甲8 小时前
什么是赫夫曼树(哈夫曼树 / Huffman Tree)
python·算法·霍夫曼树
魔镜er9 小时前
03-张量
人工智能·pytorch·python
37.2℃9959 小时前
Claude Design哪个公司技术好
python·设计模式
爱写代码的阿森9 小时前
鸿蒙三方库 | harmony-utils之RegexUtil正则匹配验证详解
服务器·华为·harmonyos·鸿蒙·huawei
1570925113410 小时前
【无标题】
开发语言·python·算法
雨的旋律209910 小时前
ubuntu2604
linux·运维·服务器
AI工具人PM产品经理10 小时前
CSDN 自动发布全流程实战:从 CKEditor HTML 注入到创作页就地发布
python
_Jimmy_10 小时前
Tool Calling 与 Function Calling 区别
人工智能·python·langchain