1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
.. _blueprints:
Blueprints
==========
Blueprints allow for modular code, they should be used whenever the
routes start to span multiple modules. Blueprints, like the app can have
template and static files, therefore a typical folder structure for a
blueprint termed ``store`` would be,
::
blueprints/
blueprints/store/__init__.py
blueprints/store/templates/
blueprints/store/templates/index.html
blueprints/store/static/
the ``__init__.py`` file should contain something like,
.. code-block:: python
from quart import Blueprint
blueprint = Blueprint('store', __name__)
@blueprint.route('/')
async def index():
return await render_template('index.html')
the endpoint is then identified as ``store.index`` for example when
using ``url_for('store.index')``.
Nested Blueprints
-----------------
It is possible to register a blueprint on another blueprint.
.. code-block:: python
parent = Blueprint("parent", __name__, url_prefix="/parent")
child = Blueprint("child", __name__, url_prefix="/child")
parent.register_blueprint(child)
app.register_blueprint(parent)
The child blueprint will gain the parent's name as a prefix to its
name, and child URLs will be prefixed with the parent's URL prefix.
.. code-block:: python
url_for('parent.child.create')
/parent/child/create
Blueprint-specific before request functions, etc. registered with the
parent will trigger for the child. If a child does not have an error
handler that can handle a given exception, the parent's will be tried.
|