0Pricing
Flask Academy · Lección

Registre Blueprints en la factoría

Conecte todos los Blueprints durante el ensamblado de la aplicación.

Registre Blueprints en la factoría es una lección gratuita de Flask Academy en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Flask Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Flask Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

The Final Assembly

With the app built and extensions bound, the last factory job is wiring up your routes. You do that by registering blueprints. 🧩

A Quick Blueprint Recall

A blueprint groups related routes into a reusable module. The factory is exactly where you attach those groups to the app.

from flask import Blueprint
main = Blueprint("main", __name__)

Register Inside create_app

Import each blueprint and call app.register_blueprint on the fresh app, all within the factory body.

def create_app():
    app = Flask(__name__)
    from .main import main
    app.register_blueprint(main)
    return app

Import Inside the Function

Notice the blueprint import sits inside create_app. This deferred import is the trick that breaks circular import chains.

Add a URL Prefix

Give a blueprint its own path namespace with url_prefix at registration time, keeping related routes neatly grouped.

app.register_blueprint(api, url_prefix="/api")

Register Several

Real apps have many blueprints. Register each one in the factory, and each contributes its own slice of routes to the app.

app.register_blueprint(auth)
app.register_blueprint(blog)
app.register_blueprint(api, url_prefix="/api")

Order Is Predictable

Because registration happens in one place, the setup order is obvious and easy to read: config, extensions, then blueprints.

Everything in One View

The factory reads like a recipe: make the app, load config, init extensions, register blueprints, return. One glance shows the whole wiring. 👀

Endpoints Get Namespaced

Blueprint endpoints are prefixed by the blueprint name, so url_for uses blueprint.view to link across modules without clashes.

url_for("main.home")

Fresh Wiring Every Time

Each call to create_app registers blueprints onto a brand new app, so tests and prod never share route state.

The Complete Pattern

Config, extensions via init_app, and blueprints via register_blueprint, all inside the factory. That is the full application factory pattern.

Quick Check

Where should blueprints be registered in the factory pattern?

Recap

You finished the pattern: register blueprints inside create_app with deferred imports and optional prefixes. Config, extensions, blueprints, return. 🏁

Preguntas frecuentes

¿La lección «Registre Blueprints en la factoría» es gratis?

Sí — el texto completo de «Registre Blueprints en la factoría» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Flask Academy, actualiza a CoddyKit PRO. El curso de Flask Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Registre Blueprints en la factoría»?

Conecte todos los Blueprints durante el ensamblado de la aplicación. Practicas Flask Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Flask Academy?

No se requiere experiencia previa. Flask Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.

¿Cuánto tiempo toma la lección «Registre Blueprints en la factoría»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Flask Academy?

Sí. Cada lección de Flask Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Problemas con un objeto app global
  2. Escriba una función create_app
  3. Inicialice extensiones con init_app
  4. Registre Blueprints en la factoría
← Volver a Flask Academy