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
相关推荐
Codefengfeng1 天前
CTF工具篇
linux·运维·服务器
清水白石0081 天前
Python 纯函数编程:从理念到实战的完整指南
开发语言·python
twilight_4691 天前
机器学习与模式识别——机器学习中的搜索算法
人工智能·python·机器学习
Jia ming1 天前
《智能法官软件项目》—罪名初判模块
python·教学·案例·智能法官
Zach_yuan1 天前
数据链路层核心技术解析:以太网与ARP协议
服务器·网络·网络协议
Jia ming1 天前
《智能法官软件项目》—法律文书生成模块
python·教学·案例·智能法官软件
曦月逸霜1 天前
Python数据分析——个人笔记(持续更新中~)
python
海棠AI实验室1 天前
第六章 从“能用”到“能交付”的关键一刀:偏好对齐(Preference Alignment)数据工程
python·私有模型训练
百锦再1 天前
Java多线程编程全面解析:从原理到实战
java·开发语言·python·spring·kafka·tomcat·maven
Jia ming1 天前
《智能法官软件项目》—法律计算器模块
python·教学·案例·智能法官