As a Flask app grows, templates and static files piling up in the root-level templates/ and static/ folders quickly turn into a mess: two modules can easily collide on index.html or style.css, and when you move a module to another project, its templates get left behind. The cleanest fix: pass template_folder and static_folder when you create a Blueprint, so each module carries its own templates and static files.
The Problem
Suppose your app has two blueprints — admin and shop — and both need an index.html page. Without separate folders, both files have to live in the same templates/ directory, but only one file can have that name. The usual workaround is long names like admin_index.html and shop_index.html: the more modules you add, the longer the filenames get, and the harder it is to tell which file belongs to which module. Static files suffer the same problem: static/style.css gives no clue whether it is the admin CSS or the shop CSS, and when you move the admin module to another project, you have to hunt down each of its files scattered across the shared folders.
# app.py — mọi template dồn vào templates/ ở gốcadmin = Blueprint("admin", __name__)shop = Blueprint("shop", __name__)@admin.route("/")def admin_index(): return render_template("index.html") # của admin hay shop?@shop.route("/")def shop_index(): return render_template("index.html") # trùng tên — chỉ một file tồn tại
The Solution
Pass template_folder and static_folder when you create the blueprint, and put its templates in a subfolder named after the blueprint:
# blueprints/admin/__init__.pyfrom flask import Blueprint, render_templateadmin = Blueprint( "admin", __name__, url_prefix="/admin", template_folder="templates", static_folder="static",)@admin.route("/")def index(): return render_template("admin/index.html")
The directory structure now looks like this:
blueprints/ admin/ __init__.py templates/ admin/ index.html static/ admin.css
In the template, point to the blueprint’s static files through the admin.static endpoint:
<link rel="stylesheet" href="{{ url_for('admin.static', filename='admin.css') }}">
A few things to remember:
- A blueprint’s template folder is added to the search path with lower priority than the app’s
templates/folder, so the app can still override a blueprint’s template when needed. - Put templates in a subfolder named after the blueprint (
admin/index.html) to avoid accidental overrides: if two blueprints both haveindex.htmlat the root of their template folder, the blueprint registered first wins. static_folderis only reachable when the blueprint has aurl_prefix— by default the file lives at/admin/static/admin.css. Without a prefix, the app’s/staticroute takes precedence and the blueprint’s static files are unreachable.url_for("admin.static", filename="admin.css")generates the correct URL even if you changeurl_prefixlater, so don’t hardcode/admin/static/...in your templates.- When Flask loads the wrong template, set
app.config["EXPLAIN_TEMPLATE_LOADING"] = Trueand Flask will print each step of its search.
Apply It to Your Project
Combined with the Application Factory, each blueprint becomes a self-contained folder holding its routes, templates, and static files — copy the whole folder to another project and it just works. Keep in the root templates/ only what the whole app shares, like base.html, macros, and error pages; split out only what belongs to a single module. A small module with just one or two templates doesn’t need its own folders — split when filenames start needing a module-name prefix just to avoid collisions.
Related Posts
- Flask: Organize Routes Neatly with Blueprints as Your Project Grows
- Flask: Use the Application Factory (create_app) So Your App Is Easy to Test and Extend from Day One
- Flask: Reuse Layouts with Template Inheritance — extends and block in Jinja2
Frequently Asked Questions
Without template_folder, where does a blueprint look for templates?
Only in the app’s templates/ folder. Passing template_folder adds the blueprint’s own folder to the search path, and the app’s folder still takes priority.
Two blueprints both have index.html — which file does Flask pick?
The app’s template wins first; among blueprints, the one registered first wins. The safest approach: put templates in a subfolder named after the blueprint, for example admin/index.html.
Why does url_for('admin.static', ...) return 404 even though the file exists?
Usually because the blueprint has no url_prefix, so the app’s /static route takes precedence. Add url_prefix="/admin" (or static_url_path) and check /admin/static/<filename> again.
Should I move all templates into blueprints?
No. Shared templates like base.html, macros, and error pages should stay in the root folder so every blueprint can inherit them; split out only templates that belong to a single module.
How do I know which file Flask is loading a template from?
Set app.config["EXPLAIN_TEMPLATE_LOADING"] = True: Flask prints each search step every time render_template runs, which is very useful when a template is being overridden and you can’t tell why.
💬 Have you tried this in your own project? If you hit an error or have a cleaner approach, leave a comment below — I read them all and will add the best ideas to the post.
References
- Flask Documentation — Modular Applications with Blueprints
