Blueprints
Blueprints organize routes, templates, static files, and error handlers by feature module — the key tool for moving a Flask project from a single file to an engineered structure. A blueprint is not an application by itself; its contents take effect only when registered, so the same blueprint can be reused across multiple app instances.
Defining a Blueprint
The first argument "blog" is the blueprint name, which becomes the endpoint namespace; url_prefix prepends /blog to all routes in the blueprint.
Registering Blueprints
Importing and registering inside the factory function (lazy imports) avoids circular import problems.
Endpoint Namespaces
Blueprint endpoints are named blueprint_name.view_name:
Blueprint Templates and Static Files
Blueprint template folders have lower priority than the app-wide templates/. The convention is to nest a same-named subdirectory inside the blueprint's template folder (templates/blog/list.html) to avoid name collisions.
Blueprint-Level Hooks and Error Handlers
Note: errors like 404 occur during route matching before any blueprint is involved, so only application-level errorhandlers catch them.
Nested Blueprints
Flask 2.0+ supports nesting blueprints — useful for versioned APIs:
Organization Advice
- Split blueprints by business domain (auth, blog, admin, api), not by technical layer.
- Give each blueprint its own package containing
views.py,models.py,forms.py, etc. - Keep blueprints free of dependencies on the app object (never
from app import app) — rely oncurrent_appinstead.