Flask中蓝图和视图介绍

版权声明:尊重他人劳动成果,转载请注明出处 https://blog.csdn.net/qq_41432935/article/details/82110224

蓝图(Blueprint)

蓝图是一种组织相关视图和其他代码的方法.使用蓝图进行注册,而不是直接在应用程序中注册视图和其他代码.

from flask import Blueprint
bp = Blueprint('auth', __name__, url_prefix='/auth')

这会创建一个Blueprint名为’auth’。与应用程序对象一样,蓝图需要知道它的定义位置,因此name 作为第二个参数传递。该url_prefix会作为与蓝图相关联的所有URL。

导入蓝图,使用app.register_blueprint()注册。在返回应用程序之前,将新代码放在工厂函数的末尾。

def create_app():
    app = ...
    # existing code omitted

    from . import auth
    app.register_blueprint(auth.bp)

    return app

猜你喜欢

转载自blog.csdn.net/qq_41432935/article/details/82110224