Blueprint
When an application has only one file, writing all the routes together is fine.
But as features grow—user module, article module, admin backend—the code becomes hard to maintain.
Blueprint is the modular solution provided by Flask that lets you split an application into independent functional units.
Why you need blueprints
The core problems blueprints solve:
- Code organization: group related routes, templates, and static files together
- Reusability: the same blueprint can be registered with different applications, or registered multiple times (with different prefixes)
- Team collaboration: different developers are responsible for different blueprint modules, reducing conflicts
Create a blueprint
A blueprint is created with the Blueprint class and is used very similarly to Flask—the same route decorators and the same way of writing view functions:
Example
File path: auth.py (user authentication blueprint)
from
flask
import
Blueprint
,
render_template
,
request
,
session
,
redirect
,
url_for
Create a blueprint instance
The first argument "auth" is the blueprint's name (used for url_for references)
name Tell the blueprint where to find templates and static files
bp
=
Blueprint
(
"auth"
,
name
)
Use bp.route instead of app.route inside the blueprint
@
bp.
route
(
"/login"
,
methods
=
[
"GET"
,
"POST"
]
)
def
login
(
)
:
if
request.
method
==
"POST"
:
username
=
request.
form
.
get
(
"username"
,
""
)
if
username:
session
[
"username"
]
=
username
return
redirect
(
url_for
(
"blog.index"
)
)
return
render_template
(
"auth/login.html"
)
@
bp.
route
(
"/register"
,
methods
=
[
"GET"
,
"POST"
]
)
def
register
(
)
:
if
request.
method
==
"POST"
:
username
=
request.
form
.
get
(
"username"
,
""
)
if
username:
return
redirect
(
url_for
(
"auth.login"
)
)
return
render_template
(
"auth/register.html"
)
@
bp.
route
(
"/logout"
)
def
logout
(
)
:
session.
clear
(
)
return
redirect
(
url_for
(
"blog.index"
)
)
Example
File path: blog.py (blog blueprint)
from
flask
import
Blueprint
,
render_template
bp
=
Blueprint
(
"blog"
,
name
)
@
bp.
route
(
"/"
)
def
index
(
)
:
Mock article data
posts
=
[
{
"title"
:
"Flask Getting Started"
,
"author"
:
"runoob"
}
,
{
"title"
:
"Blueprint Explained"
,
"author"
:
"RUNOOB"
}
,
]
return
render_template
(
"blog/index.html"
,
posts
=
posts
)
@
bp.
route
(
"/create"
,
methods
=
[
"GET"
,
"POST"
]
)
def
create
(
)
:
return
render_template
(
"blog/create.html"
)
Register the blueprint
A blueprint does not take effect automatically after creation; it needs to be registered in the application:
Example
File path: app.py
from
flask
import
Flask
from
auth
import
bp
as
auth_bp
from
blog
import
bp
as
blog_bp
app
=
Flask
(
name
)
app.
secret_key
=
"dev-secret-key"
Register the blueprint with the application
url_prefix: add a unified prefix to all routes in the blueprint
app.
register_blueprint
(
auth_bp
,
url_prefix
=
"/auth"
)
The auth blueprint's routes become: /auth/login, /auth/logout, /auth/register
app.
register_blueprint
(
blog_bp
)
The blog blueprint has no prefix, so its routes remain: /, /create
The complete route table after registration:
| URL | Blueprint | endpoint (used by url_for) |
|---|---|---|
| / | blog | blog.index |
| /create | blog | blog.create |
| /auth/login | auth | auth.login |
| /auth/register | auth | auth.register |
| /auth/logout | auth | auth.logout |
After registering a blueprint,url_for() endpoints in need to be prefixed with the blueprint name, for example url_for("auth.login") instead of url_for("login")。
Special usage of url_for in blueprints
Within the same blueprint, you can use relative references starting with a dot:
Example
Inside auth.py, using relative references is more concise
@
bp.
route
(
"/"
)
def
index
(
)
:
Relative references within the same blueprint (starting with .)
login_url
=
url_for
(
".login"
)
Equivalent to url_for("auth.login")
register_url
=
url_for
(
".register"
)
Equivalent to url_for("auth.register")
Cross-blueprint references require the full endpoint
blog_url
=
url_for
(
"blog.index"
)
Reference the blog blueprint's index
Blueprint templates and static files
A blueprint can also have its own templates and static files.
When a blueprint has its own template folder, Flask first looks in the application's templates, and if not found, then looks in the folder specified by the blueprint.
Example
Create a blueprint with a separate template folder
bp
=
Blueprint
(
"auth"
,
name
,
template_folder
=
"templates"
)
Blueprint-specific template organization
Path: auth/templates/auth/login.html
When rendering: render_template("auth/login.html")
This organization avoids filename conflicts (login.html may be different in auth and blog)
Recommended blueprint directory structure:
myflaskapp/
├── app.py # 应用入口,注册蓝图
├── auth.py # 认证蓝图
├── blog.py # 博客蓝图
├── templates/ # 应用级模板(公共 base.html 等)
│ └── base.html
├── static/ # 应用级静态文件
│ └── style.css
├── auth/ # auth 蓝图的独立资源(可选)
│ └── templates/
│ └── auth/
│ ├── login.html
│ └── register.html
└── blog/ # blog 蓝图的独立资源(可选)
└── templates/
└── blog/
├── index.html
└── create.html
The simplest approach is to not configure a separate template folder for the blueprint and instead put all templates uniformly under the project root's templates/ directory, distinguishing them with subfolders:templates/auth/login.html、templates/blog/index.html。
The factory pattern—create_app
As blueprints multiply, creating app = Flask(name) runs into circular import problems.
The factory pattern (Factory Pattern) is the best practice for solving this problem:
Example
File path: app.py
from
flask
import
Flask
def
create_app
(
)
:
"""Application factory function—returns a configured Flask instance"""
app
=
Flask
(
name
)
app.
secret_key
=
"dev-secret-key"
Load the configuration
app.
config
.
from_pyfile
(
"config.py"
,
silent
=
True
)
Import blueprints inside the function to avoid circular references
from
auth
import
bp
as
auth_bp
from
blog
import
bp
as
blog_bp
Register the blueprint
app.
register_blueprint
(
auth_bp
,
url_prefix
=
"/auth"
)
app.
register_blueprint
(
blog_bp
)
return
app
Advantages of the factory pattern:
- Eliminates circular imports: blueprints can import app and app can import blueprints, because the imports happen inside the function
- Supports testing: you can create app instances with different configurations for testing
- Multi-instance deployment: the same code can create multiple independent app instances
After using the factory pattern, start with flask --app "app:create_app()" run:
$ flask --app "app:create_app()" run
Other features in blueprints
Blueprints also support decorators such as before_request and errorhandler, used the same way as with app:
Example
@
bp.
before_request
def
check_login
(
)
:
"""Check login status before all requests in the auth blueprint"""
if
request.
endpoint
not
in
(
"auth.login"
,
"auth.register"
)
:
if
"username"
not
in
session:
return
redirect
(
url_for
(
"auth.login"
)
)
@
bp.
errorhandler
(
404
)
def
not_found
(
error
)
:
"""404 error page dedicated to the auth blueprint"""
return
render_template
(
"auth/404.html"
)
,
404
A blueprint's before_request only affect routes within that blueprint. If you need to execute logic before all requests, you should use app.before_request。