← Back to Python

All Topics

Advertisement

Learn/Python/Web Development

Flask Blueprints

Topic: Flask

Advertisement

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

  1. Create modular user management blueprint
  2. Organize routes by feature
  3. Share templates between blueprints
  4. Use url_prefix for grouping
  5. Implement blueprint before request hooks

Advertisement

Advertisement

Need More Practice?

Get personalized Python help from ChatWhole's AI-powered platform.

Get Expert Help →