flask 蓝图介绍;
2.蓝图项目相关目录结构;
3.蓝图相关模块代码
(1).项目入口启动代码;
from flask import Flask from ops01.view import app01 from ops02.view import app02 app =Flask(__name__) app.register_blueprint(app01,url_prefix='/app01') app.register_blueprint(app02,url_prefix='/app02') if __name__ == '__main__': app.run( )
(2).APP01 view 代码;
from flask import Blueprint from flask import request, Response import json app01 = Blueprint('app01', __name__) @app01.route('/api/v1', methods=['GET', 'POST', '[DELETE]']) def mainApp0v1(): data = Response(json.dumps({"code": 0, "data": "方法不允许"}), mimetype='application/json') if request.method == "GET": return QueryNamev1() else: return data def QueryNamev1(): data = "我是测试app01项目-api.v1版本" return Response(json.dumps({"code": 0, "data": data,"status":"success"}), mimetype='application/json')
(3).APP02 view 代码;
from flask import Blueprint from flask import request, Response app02= Blueprint('app02', __name__) import json @app02.route('/api/v1', methods=['GET', 'POST','[DELETE]']) def mainAppv1(): data = Response(json.dumps({"code": 0, "data": "方法不允许"}), mimetype='application/json') if request.method == "GET": return QueryName() else: return data def QueryName(): data = "我是测试app02项目" return Response(json.dumps({"code": 0, "data": data, "status": "success"}), mimetype='application/json')
(4).蓝图(postman客户端测试):
4.1 APP 01 测试;
4.2 APP02 测试;
5.针对同项目不同访问路由访问;
5.1代码示例;
from flask import Blueprint from flask import request, Response import json app01 = Blueprint('app01', __name__) @app01.route('/api/v1', methods=['GET', 'POST', '[DELETE]']) def mainApp0v1(): data = Response(json.dumps({"code": 0, "data": "方法不允许"}), mimetype='application/json') if request.method == "GET": return QueryNamev1() else: return data def QueryNamev1(): data = "我是测试app01项目-api.v1版本" return Response(json.dumps({"code": 0, "data": data,"status":"success"}), mimetype='application/json') @app01.route('/api/v2', methods=['GET', 'POST', '[DELETE]']) def mainAppv2(): data = Response(json.dumps({"code": 0, "data": "方法不允许"}), mimetype='application/json') if request.method == "GET": return QueryNamev2() else: return data def QueryNamev2(): data = "我是测试app01项目-api.v2 版本" return Response(json.dumps({"code": 0, "data": data,"status":"success"}), mimetype='application/json')
5.2 Postman 测试结果;
api v1版本
api v2版本