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
相关推荐
kobe_OKOK_7 小时前
DRF接口幂等操作
python·django
huainingning7 小时前
R4930-G7服务器(RAID-LSI-9560-LP-8i-4GB)网页方式做Raid方法
服务器
廿士7 小时前
python脚本使用相关
python
青 春 记 忆8 小时前
零基础入门python19:Flask账本第一步——应用工厂、蓝图和健康检查
python·flask·后端开发
朦胧之8 小时前
Python 后端核心知识
python
denggun123458 小时前
yield
前端·数据库·python
zx_741484819 小时前
【Python 入门】面向对象基础:类、对象、成员变量与构造方法
开发语言·python
招财小梗9 小时前
沈阳商贸公司AI企业服务优势几何?
大数据·人工智能·python
小杨不想秃头9 小时前
信息安全工程师教程第二章密码学
运维·服务器·密码学
I'mChloe10 小时前
WGCLOUD怎么监控多台服务器?从Server、Agent 部署到监控面板实战
运维·服务器