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
相关推荐
xiaoshuaishuai824 分钟前
Git二分法定位Bug
开发语言·python
2401_8357925432 分钟前
FastAPI 速通
windows·python·fastapi
捧月华如1 小时前
Linux 系统性能压测工具全景指南(含工程实战)
linux·运维·服务器
YMWM_1 小时前
export MPLBACKEND=Agg命令使用
linux·python
s19134838482d1 小时前
vlan实验报告
运维·服务器·网络
派大星~课堂1 小时前
【力扣-148. 排序链表】Python笔记
python·leetcode·链表
微涼5301 小时前
【Python】在使用联网工具时需要的问题
服务器·python·php
想唱rap1 小时前
线程的同步与互斥
linux·运维·服务器·数据库·mysql
小白菜又菜1 小时前
Leetcode 657. Robot Return to Origin
python·leetcode·职场和发展
2402_881319302 小时前
跨服务通信兜底机制-Java 回传失败无持久重试队列,报告可能静默丢失。
java·开发语言·python