Flask Blueprints

Reviewed & published by Brayan K

A Blueprint is a self-contained collection of routes, templates, and static files that you define separately and then register on your Flask application, letting you split a growing project into clean, reusable modules.

Part of the free Flask course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.

In this lesson you'll create your first Blueprint, register it on the app, give it a URL prefix, and learn how Blueprints keep large Flask projects organized.

What You'll Learn in This Lesson

1 Why Blueprints Exist

A Blueprint is a reusable group of routes that you define independently from the application, then attach to it later. Think of it as a mini-application for one feature.

When every route lives in a single app.py, the file grows until it is impossible to navigate. Blueprints let you split your project by feature — one module for authentication, one for the blog, one for the admin panel — and register each on the main app.

🧩 A Blueprint gives you:

2 Creating and Registering a Blueprint

You create a Blueprint with Blueprint(name, __name__), define routes on it using @bp.route instead of @app.route, and finally call app.register_blueprint(bp) on your application.

The code below builds a small blog Blueprint and wires it into the app:

# app.py — a Blueprint registered on the application
from flask import Flask, Blueprint

# 1. Create a Blueprint named "blog".
#    The first argument is the Blueprint's name; __name__ helps
#    Flask locate its templates and static files.
blog_bp = Blueprint("blog", __name__)

# 2. Define routes ON the Blueprint with @blog_bp.route
@blog_bp.route("/")
def index():
    return "Blog home"

@blog_bp.route("/post/<int:post_id>")
def show_post(post_id):
    return f"Showing post {post_id}"

# 3. Create the app and register the Blueprint.
app = Flask(__name__)
app.register_blueprint(blog_bp)

# Print every registered route to confirm the Blueprint attached.
for rule in app.url_map.iter_rules():
    print(rule.endpoint, "->", rule)

# ✅ Expected output:
# static -> /static/<path:filename>
# blog.index -> /
# blog.show_post -> /post/<int:post_id>

Notice the endpoint names print as blog.index and blog.show_post — the Blueprint name becomes a prefix on the endpoint, which is how you reference them in url_for. Run the real app with flask run to serve these pages.

3 Adding a URL Prefix

Pass url_prefix when registering a Blueprint to mount all of its routes under one path. A route defined at / on a Blueprint registered with url_prefix="/blog" is served at /blog/.

# app.py — registering a Blueprint under a URL prefix
from flask import Flask, Blueprint

blog_bp = Blueprint("blog", __name__)

@blog_bp.route("/")
def index():
    return "Blog home"          # served at /blog/

@blog_bp.route("/about")
def about():
    return "About the blog"     # served at /blog/about

app = Flask(__name__)

# Every blog route is now grouped under /blog
app.register_blueprint(blog_bp, url_prefix="/blog")

for rule in app.url_map.iter_rules():
    print(rule)

# ✅ Expected output:
# /static/<path:filename>
# /blog/
# /blog/about

Prefixes keep features isolated: the blog lives under /blog, an admin Blueprint under /admin, and so on — with no route collisions.

🔬 Anatomy of a Blueprint

LineWhat it does
Blueprint("blog", __name__)Creates a Blueprint named "blog"
@blog_bp.route("/")Defines a route on the Blueprint
app.register_blueprint(blog_bp)Attaches the Blueprint to the app
url_prefix="/blog"Mounts every route under /blog

🎯 Your Turn: Fill in the Blanks

Complete the code below. Replace each ___ so it creates an auth Blueprint, defines a login route, and registers it under /auth.

# 🎯 YOUR TURN: replace each ___ to finish the Blueprint
from flask import Flask, ___          # import the Blueprint class

auth_bp = ___("auth", __name__)       # create the Blueprint

@auth_bp.___("/login")                # define a route ON the Blueprint
def login():
    return "Login page"

app = Flask(__name__)
app.___(auth_bp, url_prefix="___")    # register it under /auth

for rule in app.url_map.iter_rules():
    print(rule)

Common Errors & Quick Reference

❌ My Blueprint routes return 404

You probably forgot to call app.register_blueprint(bp). Defining routes on a Blueprint does nothing until the Blueprint is registered on the app.

❌ Could not build url for endpoint 'index'

Blueprint endpoints are namespaced. Use the Blueprint name as a prefix in url_for, like url_for("blog.index"), not just "index".

TaskCommand / Code
Create a Blueprintbp = Blueprint("name", __name__)
Define a route@bp.route("/")
Register itapp.register_blueprint(bp)
Add a prefixregister_blueprint(bp, url_prefix="/x")

🏆 Mini Challenge: Two Blueprints

Build an app with two Blueprints so each feature has its own namespace.

# 🏆 Mini Challenge: an app with two Blueprints
from flask import Flask, Blueprint

main_bp = Blueprint("main", __name__)
shop_bp = Blueprint("shop", __name__)

@main_bp.route("/")
def home():
    return "Welcome home"

@shop_bp.route("/products")
def products():
    return "Our products"        # served at /shop/products

app = Flask(__name__)
app.register_blueprint(main_bp)
app.register_blueprint(shop_bp, url_prefix="/shop")

for rule in app.url_map.iter_rules():
    print(rule)

❓ Frequently Asked Questions

Lesson complete — your projects can now scale!

You can create a Blueprint, define routes on it, register it on the app, and group routes under a URL prefix. This is the foundation of every well-structured Flask project.

Practice quiz

What is a Flask Blueprint?

  • A reusable group of routes you register on an app
  • A database migration tool
  • A template inheritance system
  • A type of config class

Answer: A reusable group of routes you register on an app. A Blueprint groups related routes, templates, and static files into a reusable component.

How do you create a Blueprint?

  • Flask('blog')
  • Blueprint('blog', __name__)
  • app.blueprint('blog')
  • register('blog')

Answer: Blueprint('blog', __name__). Create one with Blueprint(name, __name__).

Which decorator defines a route on a Blueprint named bp?

  • @app.route
  • @route.bp
  • @bp.route
  • @bp.view

Answer: @bp.route. Define routes on the Blueprint with @bp.route instead of @app.route.

How do you attach a Blueprint to the application?

  • bp.attach(app)
  • app.add_blueprint(bp)
  • bp.register(app)
  • app.register_blueprint(bp)

Answer: app.register_blueprint(bp). Call app.register_blueprint(bp) to attach it.

How do you mount every Blueprint route under /blog?

  • register_blueprint(bp, url_prefix='/blog')
  • Blueprint('blog', prefix='/blog')
  • bp.route('/blog')
  • app.prefix('/blog')

Answer: register_blueprint(bp, url_prefix='/blog'). Pass url_prefix when registering: register_blueprint(bp, url_prefix='/blog').

With a blog Blueprint, what is the endpoint name of its index view?

  • index
  • blog/index
  • blog.index
  • app.blog.index

Answer: blog.index. The Blueprint name prefixes the endpoint, so it becomes blog.index.

If Blueprint routes return 404, what did you likely forget?

  • To call app.register_blueprint(bp)
  • To set DEBUG=True
  • To import jsonify
  • To add a SECRET_KEY

Answer: To call app.register_blueprint(bp). Routes do nothing until the Blueprint is registered on the app.

How do you build a URL for a Blueprint endpoint?

  • url_for('index')
  • url_for('blog.index')
  • url_for(blog, index)
  • url_for('/blog/index')

Answer: url_for('blog.index'). Use the namespaced endpoint, like url_for('blog.index').

Why do production Flask apps use Blueprints?

  • They are required by Flask
  • They speed up the database
  • They split features into clean, reusable modules
  • They replace SECRET_KEY

Answer: They split features into clean, reusable modules. Blueprints keep large projects organized by splitting features into modules.

What is the second argument to Blueprint('blog', __name__) for?

  • The URL prefix
  • The route methods
  • The database name
  • Helping Flask locate templates and static files

Answer: Helping Flask locate templates and static files. __name__ helps Flask locate the Blueprint's templates and static files.

Continue this course

Frequently asked questions

What is a Flask Blueprint?

A Blueprint is a way to group related routes, templates, and static files into a reusable component. You define routes on the Blueprint, then register it on your application, which keeps large projects organized.

Why use Blueprints instead of one big app.py?

As an app grows, putting every route in one file becomes hard to maintain. Blueprints let you split features (auth, blog, admin) into separate modules that you register on the main app, improving structure and reuse.

How do I register a Blueprint?

Create the Blueprint with Blueprint(name, __name__), define routes on it with the @bp.route decorator, then call app.register_blueprint(bp) on your application object before you run flask run.

Can a Blueprint have a URL prefix?

Yes. Pass url_prefix when registering, like app.register_blueprint(bp, url_prefix='/blog'). Every route on that Blueprint is then served under /blog, so a route at '/' becomes /blog/.

Related lessons