使用蓝图模块化项目


1、使用Blueprint(蓝图)可以将大型项目中的代码进行模块化

2、例如将项目中不同的URL和视图函数分别保存到对应的Python文件

3、app.py文件可以只用于引用蓝图和注册蓝图

# article.py

from flask import Blueprint, render_template

article_blue = Blueprint('article', __name__)

@article_blue.route('/article/')
def article():
    return render_template('article.html')

# login.py

from flask import Blueprint, render_template

login_blue = Blueprint('login', __name__)

@login_blue.route('/login/')
def login():
    return render_template('login.html')

# index.py

from flask import Blueprint, render_template

index_blue = Blueprint('index', __name__)

@index_blue.route('/')
def index():
    return render_template('index.html')

# app.py

from flask import Flask
from apps.index import index_blue
from apps.login import login_blue
from apps.article import article_blue

app = Flask(__name__)
app.register_blueprint(index_blue)
app.register_blueprint(login_blue)
app.register_blueprint(article_blue)

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

results matching ""

    No results matching ""