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
相关推荐
二川bro5 分钟前
Python模型优化实战:深度学习加速与压缩技巧
python
l***749426 分钟前
SQL Server2022版+SSMS安装教程(保姆级)
后端·python·flask
石像鬼₧魂石1 小时前
如何使用Kali Linux自带字典进行密码破解?
linux·运维·服务器
傻啦嘿哟1 小时前
Python实现PDF文档高效转换为HTML文件:从基础到进阶的完整指南
python·pdf·html
天下无敌笨笨熊2 小时前
ES作为向量库研究
大数据·python·elasticsearch
数据知道2 小时前
FastAPI项目:从零到一搭建一个网站导航系统
python·mysql·fastapi·python web·python项目
咸鱼の猫3 小时前
用samba服务器将虚拟机的Ubuntu(磁盘)映射到本地电脑实现文件互传
linux·服务器·ubuntu
程序员爱钓鱼3 小时前
Python 编程实战 · 进阶与职业发展:数据分析与 AI(Pandas、NumPy、Scikit-learn)
后端·python·trae
软件开发技术深度爱好者3 小时前
Python库/包/模块管理工具
开发语言·python
wanhengidc3 小时前
网站服务器具体是指什么
运维·服务器