Introduction
Blueprints organize Flask applications into modular components.
Creating Blueprints
# app/auth/routes.py
from flask import Blueprint, render_template
bp = Blueprint("auth", __name__, url_prefix="/auth")
@bp.route("/login")
def login():
return render_template("auth/login.html")
@bp.route("/register")
def register():
return render_template("auth/register.html")
Registering Blueprints
# app/__init__.py
from flask import Flask
from app.auth import bp as auth_bp
def create_app():
app = Flask(__name__)
app.register_blueprint(auth_bp)
return app
URL Generation
# Generate URLs with blueprint
url = url_for("auth.login")
# Redirect with blueprints
return redirect(url_for("auth.login"))
Practice Problems
- Create modular user management blueprint
- Organize routes by feature
- Share templates between blueprints
- Use url_prefix for grouping
- Implement blueprint before request hooks