我如何使用蓝图将所有路由拆分为子模块

时间:2015-10-26 11:46:20

标签: python flask

这是我的回购https://github.com/poc7667/flask_blueprint

我想将所有路由分成子模块,

我将在根文件夹

中运行python web.py

我希望我可以通过/hihi

访问sub_1模块下的路由http://localhost:8888/hihi

但是我得到了Not found error

web.py

from flask import Flask, Response, request, url_for
app = Flask(__name__)
from sub_1 import sub_1_mod
app.register_blueprint(sub_1_mod, url_prefix="/")
if __name__ == "__main__":
    app.run(debug=True, host='localhost', port=8888)

sub_1 / web.py

# -*- coding: utf8 -*-
from sub_1 import sub_1_mod
from flask import Flask, Response, request
app = Flask(__name__)
@app.route("/hihi", methods=['GET'])
def hihi():
    return make_response(["hihi"], VERSION)

sub_1 /的初始化的.py

from flask import Blueprint
sub_1_mod = Blueprint('sub_1', __name__)
import sub_1.web

异常

Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

1 个答案:

答案 0 :(得分:1)

sub_1 / web.py 中,请勿创建其他Flask应用程序。而是将路线添加到现有的blueprint

from sub_1 import sub_1_mod
@sub_1_mode.route("/hihi", methods=['GET'])
def hihi():
    return make_response(["hihi"], VERSION)