Managing Blueprint Resources
To manage blueprint-specific resources like static files and templates, you configure the Blueprint class with specific folder paths and URL prefixes. This allows you to isolate assets for different parts of your application.
Configure Blueprint Resources
When initializing a Blueprint, you can specify where its static files and templates are located. These paths are relative to the blueprint's root_path (the directory containing the module where the blueprint is defined).
from flask import Blueprint, render_template
# Define a blueprint with its own resources
admin = Blueprint(
"admin",
__name__,
url_prefix="/admin",
template_folder="templates",
static_folder="static",
)
@admin.route("/")
def index():
# Renders from the blueprint's template folder
return render_template("admin/index.html")
Serve Static Files
If a static_folder is provided, Flask automatically registers a route to serve files from that directory. By default, the URL path matches the folder name (e.g., /admin/static).
To generate URLs for blueprint static files, use url_for with the blueprint name followed by .static:
from flask import url_for
# In a view or template
static_url = url_for("admin.static", filename="css/style.css")
# Returns: /admin/static/css/style.css
Organize Templates to Avoid Collisions
Blueprint templates are added to the application's template search path. However, the application's main templates folder always takes precedence. If two blueprints have a template with the same name (e.g., index.html), the one registered first or the one in the app folder will be used.
To prevent collisions, nest your templates inside a subfolder named after the blueprint:
- Structure:
your_package/admin/templates/admin/index.html - Usage:
render_template("admin/index.html")
Manage Nested Blueprints
You can register blueprints on other blueprints. URL prefixes and subdomains are concatenated during registration.
parent = Blueprint("parent", __name__)
child = Blueprint("child", __name__)
@child.route("/view")
def child_view():
return "Child View"
# Register child on parent
parent.register_blueprint(child, url_prefix="/child")
# Register parent on app
app.register_blueprint(parent, url_prefix="/parent")
# The route is now accessible at /parent/child/view
Access Internal Resources
Use the open_resource method to read files relative to the blueprint's root path. This is useful for reading configuration files or data assets bundled with the blueprint.
@admin.route("/config")
def show_config():
# Opens 'data/config.json' relative to the admin blueprint folder
with admin.open_resource("data/config.json") as f:
config_data = f.read()
return config_data
Troubleshooting
- Static Files Not Found: If your blueprint does not have a
url_prefix, the application's static route will take precedence. Always ensure blueprints with static folders have a uniqueurl_prefixor a customstatic_url_path. - Wrong Template Rendered: If the wrong template is being displayed, check if another blueprint or the main app has a template with the same path. Use the subfolder nesting pattern described above to ensure uniqueness.
- Registration Errors: You cannot call setup methods like
routeorerrorhandlerafter the blueprint has been registered on the application. Ensure all configuration is complete before callingapp.register_blueprint().