整个项目分成两个部分,一个是启动文件,一个是项目文件App。
manage.py(项目入口)
from flask_script import Manager
from App.init import create_app
app = create_app()
manager = Manager(app=app)
if __name__ == '__main__':
manager.run()
App/init.py(app初始化文件)懒加载模式
from flask import Flask
from App.views import init_route
def create_app():
app = Flask(__name__)
init_route(app)
return app
App/views.py(路由管理文件)懒加载模式
def init_route(app):
@app.route("/")
def index():
return "<h1>hello flask!!</h1>"
App/init.py(app初始化文件)蓝图注册模式
from flask import Flask
from App.views import blue
def create_app():
app = Flask(__name__)
app.register_blueprint(blue)
return app
App/views.py(路由管理文件)蓝图注册模式
from flask import Blueprint
blue = Blueprint('blue', __name__)
@blue.route('/')
def index():
return "hello 蓝图"