Flask--蓝图

蓝图(blue print)

类似Django中的不同APP,实现分目录实现业务功能。

目录结构:

|flaskbp
|——flaskbp
|————static
|————templates
|————views
|——————bp1.py
|——————bp2.py
|——————__init__.py
|——————manage.py

manage.py

from flaskbp import create_app

app = create_app()

if __name__ == '__main__':
    app.run()

__init__.py

from flask import Flask
from flaskbp.views.bp1 import b1
from flaskbp.views.bp2 import b2


def create_app():
    app = Flask(__name__)
    app.secret_key = "a1b2c3"

    @app.route("/index")
    def index():
        return "index"

    app.register_blueprint(b1)
    app.register_blueprint(b2)

    app.register_blueprint(b1,url_prefix='web')		# 还可以定制前缀
    app.register_blueprint(b2,url_prefix='api')

    return app

Bp1.py

from flask import Blueprint

b1 = Blueprint("b1",__name__)

@b1.route("/a1")
def a1():
    return "a1"

@b1.route("/a2")
def a2():
    return "a2"

Bp2.py

from flask import Blueprint

b2 = Blueprint("b2",__name__)

@b2.route("/a3")
def a3():
    return "a3"

@b2.route("/a4")
def a4():
    return "a4"
posted @ 2019-11-21 19:43  忘川的彼岸  阅读(141)  评论(0编辑  收藏  举报