官术网_书友最值得收藏!

Blueprint

Before diving into a detailed description of each file, let's have a look at the instantiation Flask mechanism. As you already know, we are creating a submodule as an independent module under the root module. Flask introduces the concept of blueprint for making the submodule components under a common pattern.

The Flask blueprint instance is very similar to a Flask instance, but it's not an application object. Instead, it has the ability to construct and extend the parent application. With the help of blueprint, you can design a modular application.

The following is a code snippet of the Blueprint instantiation in the auth/__init__.py file:

from flask import Blueprint
auth = Blueprint('auth', __name__)
from . import views

As you can see, it has very similar characteristics to the Flask class and follows a similar pattern. Now, we will be using the auth instance of the blueprint in views to register routes. To execute the application, we need to bind the blueprint object with the Flask application instance.

The following is the code snippet from the app/__init__.py file where we are going to create the Flask application instance:

from .auth import auth as auth_blueprint
from app.config import config

app = Flask(__name__)
app.config.from_object(config[environment])

app.register_blueprint(auth_blueprint, url_prefix='/auth')

With the help of the register_blueprint method, we are registering the auth module blueprint and we can add the URL prefix as well. We will have the complete description of this file after we look at the todo module explanation.

主站蜘蛛池模板: 乌兰县| 那曲县| 论坛| 军事| 泸水县| 龙州县| 岳西县| 旅游| 麻栗坡县| 宣汉县| 阳春市| 大洼县| 南和县| 滦平县| 曲周县| 夏河县| 和顺县| 安丘市| 吐鲁番市| 天门市| 札达县| 新竹县| 嫩江县| 磐安县| 黑河市| 图木舒克市| 金乡县| 平舆县| 花莲市| 石嘴山市| 黄浦区| 梅州市| 卢氏县| 南涧| 禹城市| 昌黎县| 宁陵县| 嫩江县| 正宁县| 民权县| 桃源县|