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
相关推荐
MediaTea1 小时前
Python:生成器表达式详解
开发语言·python
-To be number.wan1 小时前
Python数据分析:SciPy科学计算
python·学习·数据分析
Dxy12393102161 小时前
DataFrame数据修改:从基础操作到高效实践的完整指南
python·dataframe
overmind3 小时前
oeasy Python 115 列表弹栈用pop删除指定索引
开发语言·python
暴力求解3 小时前
Linux---进程(五)进程调度
linux·运维·服务器
wsad05324 小时前
Linux 用户和组管理完整指南(中英文参数对照)
linux·运维·服务器
hnxaoli4 小时前
win10程序(十六)通达信参数清洗器
开发语言·python·小程序·股票·炒股
电饭叔4 小时前
文本为 “ok”、前景色为白色、背景色为红色,且点击后触发 processOK 回调函数的 tkinter 按钮
开发语言·python
EmbedLinX4 小时前
嵌入式Linux之U-Boot
linux·服务器·笔记·学习
雷电法拉珑5 小时前
财务数据批量采集
linux·前端·python