ブループリント#
ブループリントではモジュール式のコードを許可し、ルートが複数のモジュールにまたがることがないようにするために使用されます。アプリなどのブループリントにはテンプレートファイルと静的ファイルを含めることができるため、storeと呼ばれるブループリントの典型的なフォルダ構造は、
blueprints/
blueprints/store/__init__.py
blueprints/store/templates/
blueprints/store/templates/index.html
blueprints/store/static/
the __init__.pyファイルには次のような内容が含まれている必要があります。
from quart import Blueprint
blueprint = Blueprint('store', __name__)
@blueprint.route('/')
async def index():
return await render_template('index.html')
次に、エンドポイントはstore.indexなど、url_for('store.index')を使用するときに識別されます。
ネストされたブループリント#
別のブループリントにブループリントを登録できます。
parent = Blueprint("parent", __name__, url_prefix="/parent")
child = Blueprint("child", __name__, url_prefix="/child")
parent.register_blueprint(child)
app.register_blueprint(parent)
子ブループリントは、親の名前をその名のプレフィックスとして取得し、子のURLは親のURLプレフィックスでプレフィックスされます。
url_for('parent.child.create')
/parent/child/create
親に登録されたブループリント固有のビフォアリクエスト関数などが子にトリガーされます。子に特定の例外を処理できるエラーハンドラがない場合、親のハンドラが試されます。