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 小时前
Linux 常用服务器命令
linux·运维·服务器
sg_knight1 小时前
对象池模式(Object Pool)
python·设计模式·object pool·对象池模式
敲代码的哈吉蜂1 小时前
Haproxy
linux·运维·服务器
240291003371 小时前
自编码器(AE)与变分自编码器(VAE)-- 认识篇
python·神经网络·机器学习
敲代码的哈吉蜂1 小时前
haproxy的算法——混合算法
linux·运维·服务器·算法
市安1 小时前
构建HTTPS服务镜像
linux·运维·服务器
郝学胜-神的一滴1 小时前
Python中的“==“与“is“:深入解析与Vibe Coding时代的优化实践
开发语言·数据结构·c++·python·算法
hhzz2 小时前
云服务器ECS的高可用部署方案----弹性公网IP和负载均衡的操作
服务器·tcp/ip·负载均衡·ecs·云服务器
一个处女座的程序猿O(∩_∩)O2 小时前
Python多重继承详解
开发语言·python