当前位置:网站首页>Flask contexts, blueprints and Flask-RESTful
Flask contexts, blueprints and Flask-RESTful
2022-08-02 14:21:00 【czy1206527605】
一.上下文
1.请求上下文
request 请求上下文 请求对象,封装了客户端发出的HTTP请求中的内容 session 请求上下文
用户会话,用于存储请求之间需要“记住”的*的字典
2.应用上下文
current_app 应用上下文 当前激活程序的程序实例 g 应用上下文
处理请求时用作临时存储的对象.每次请求都会重设这个变量
二.蓝图
1.什么是蓝图
蓝图(blueprint):用于实现单个应用的视图、模板、静态文件的集合.
蓝图就是模块化处理的类.类似于django中app,子应用.
简单来说,蓝图就是一个存储操作路由映射方法的容器,主要用来实现客户端请求和URL相互关联的功能.
在Flask中,使用蓝图可以帮助我们实现模块化应用的功能.flask中,将项目模块化,blueprint,是flask自带的一种开发模式,目的是为了方便开发大型的项目
2.蓝图的实现
Top 1.创建蓝图
from flask import Blueprint
users_bp = Blueprint('users', __name__)
# url_prefix # 指定当前蓝图下所有路由的前缀
@users_bp.route('/index')
def indext():
return 'users bp response'
Top 2.注册蓝图
from flask import Flask
from apps.users_bp import users_bp
app = Flask(__name__)
app.register_blueprint(users_bp)
if __name__ == '__main__':
app.run()
三.Flask-RESTful
Flask-RESTful 提供的最主要的基础就是资源(resources).资源(Resources)是构建在 Flask 可拔插视图 之上,只要在你的资源(resource)上定义方法就能够容易地访问多个 HTTP 方法
1.安装Flask-RESTful
pip install flask-restful
2.运用Flask-RESTful
from flask import Flask
from flask_restful import Api, Resource
app = Flask(__name__)
api = Api(app)
# custom class view, 必须继承于Resource
class IndexResource(Resource):
def get(self):
return 'first restful api'
api.add_resource(IndexResource, '/index', endpoint='index')
if __name__ == '__main__':
app.run(debug=True)
3.蓝图与Flask-RESTful
蓝图区
from flask import Blueprint
from flask_restful import Api, Resource
users_bp = Blueprint('users', __name__)
api = Api(users_bp)
class IndexResource(Resource):
def get(self):
return 'users_bp.api'
api.add_resource(IndexResource, '/index', endpoint='index')
注册app
from flask import Flask
from flask_restful import Api, Resource
from apps.users_bp import users_bp
app = Flask(__name__)
api = Api(app)
app.register_blueprint(users_bp)
if __name__ == '__main__':
app.run()
边栏推荐
- [ROS](04)package.xml详解
- [ROS] The software package of the industrial computer does not compile
- 瑞吉外卖笔记——第08讲读写分离
- 第七单元 ORM表关系及操作
- The 2nd China Rust Developers Conference (RustChinaConf 2021~2022) Online Conference Officially Opens Registration
- Sentinel源码(一)SentinelResourceAspect
- Flask-RESTful request response and SQLAlchemy foundation
- ping命令的使用及代码_通过命令查看ping路径
- The language of initial
- Unit 12 associated serialization
猜你喜欢
创建ROS工作空间](/img/2a/11e5023ef6d052d98b4090d2eea017.png)
[ROS](01)创建ROS工作空间

Sentinel源码(六)ParamFlowSlot热点参数限流

Unit 15 Paging, Filtering

St. Regis Takeaway Notes - Lecture 10 Swagger

动手学ocr(一)

第十三单元 混入视图基类

Building and getting started with the Flask framework

(ROS) (03) CMakeLists. TXT, rounding
![[ROS] The software package of the industrial computer does not compile](/img/a8/a1347568170821e8f186091b93e52a.png)
[ROS] The software package of the industrial computer does not compile

第十单元 前后连调
随机推荐
drf源码分析与全局捕获异常
PHP open source customer service system _ online customer service source code PHP
Linux: CentOS 7 install MySQL5.7
paddle window10环境下使用conda安装
Sentinel源码(二)入口方法分析
Steps to connect the virtual machine with xshell_establish a network connection between the host and the vm virtual machine
Briefly write about the use and experience of PPOCRLabel
瑞吉外卖笔记——第05讲Redis入门
8576 Basic operations of sequential linear tables
第十单元 前后连调
第十一单元 序列化器
redis分布式锁和看门狗的实现
Deep learning framework pytorch rapid development and actual combat chapter3
Building and getting started with the Flask framework
海明校验码纠错设计原理
深度学习框架pytorch快速开发与实战chapter4
动手学ocr(一)
MySQL数据库设计规范
Cloin 控制台乱码
window10下半自动标注