当前位置:网站首页>Flask上下文,蓝图和Flask-RESTful
Flask上下文,蓝图和Flask-RESTful
2022-08-02 14:01: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)
# 自定义类视图, 必须继承于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()
边栏推荐
- ping命令的使用及代码_通过命令查看ping路径
- Gstreamer Plugin注册流程详解
- Supervision strikes again, what about the market outlook?2021-05-22
- 劲爆!阿里巴巴面试参考指南(嵩山版)开源分享,程序员面试必刷
- 期货具体是如何开户的?
- What is the difference between web testing and app testing?
- 打破文件锁限制,以存储力量助力企业增长新动力
- Go语言初始
- rust使用mysql插入数据
- els long block deformation conditions, boundary collision judgment
猜你喜欢

智能指针-使用、避坑和实现

Some impressions of the 519 plummet 2021-05-21

Break the limit of file locks and use storage power to help enterprises grow new momentum

Supervision strikes again, what about the market outlook?2021-05-22

About the development forecast of the market outlook?2021-05-23

关于市场后市的发展预测? 2021-05-23

此次519暴跌的几点感触 2021-05-21

二分查找 && 树

RKMPP 在FFmpeg上实现硬编解码

FreeBSD bnxt以太网驱动源码阅读记录三:
随机推荐
Haystack的介绍和使用
rpm包的卸载与安装[通俗易懂]
Break the limit of file locks and use storage power to help enterprises grow new momentum
Interviewer: Can you talk about optimistic locking and pessimistic locking?
【学习笔记】数位dp
大而全的pom文件示例
RKMPP库快速上手--(一)RKMPP功能及使用详解
Raft协议图解,缺陷以及优化
tinymce 如何实现动态国际化
Sentinel源码(一)SentinelResourceAspect
C# 编译错误:Compiler Error CS1044
How to solve 1045 cannot log in to mysql server
关于Google词向量模型(googlenews-vectors-negative300.bin)的导入问题
shell脚本“画画”
rhce第三天作业
Flutter 实现光影变换的立体旋转效果
CVE-2020-27986 (Sonarqube sensitive information leak) vulnerability fix
【Tensorflow】AttributeError: module 'keras.backend' has no attribute 'tf'
els 长条方块变形条件、边界碰撞判定
What are the file encryption software?Keep your files safe