0Pricing
Flask Academy · Lección

Cree y registre un Blueprint

Defina un Blueprint y asígnelo a la aplicación.

Cree y registre un Blueprint es una lección gratuita de Flask Academy en CoddyKit. Esta es la lección 2 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.

Two Steps to a Blueprint

Working with a blueprint is two moves: first you define it in its own module, then you register it on the app. Let us walk through both.

Import the Blueprint Class

Everything starts with one import. The Blueprint class comes straight from flask, just like the Flask app object does.

from flask import Blueprint

Create the Blueprint Object

You build a blueprint by giving it a name and import_name. The name uniquely identifies this blueprint inside your whole app.

blog = Blueprint("blog", __name__)

What __name__ Does

That second argument, __name__, tells the blueprint where it lives so Flask can later find its templates and static files.

Routes on the Blueprint

Inside the module you attach views with @blog.route instead of @app.route. Same idea, but bound to the blueprint.

@blog.route("/posts")
def posts():
    return "All posts"

Still Dormant for Now

Defining routes is not enough. Until you register the blueprint, none of its URLs respond. The blueprint is inert on its own.

Import It in the App

Over in your app setup, you import the blueprint object from its module so the app can take ownership of it.

from blog.routes import blog

Register on the App

One call wires everything up. register_blueprint copies the blueprint routes onto the app and makes them live.

app.register_blueprint(blog)

Order of Operations

Create the app, import the blueprint, then register it. Get this sequence right and your feature routes spring to life. ✅

Endpoint Names Get Namespaced

A view named posts inside the blog blueprint is referenced as blog.posts. The blueprint name becomes a tidy prefix for endpoints.

Avoiding Circular Imports

Keep the blueprint in its own file and import it where you build the app. This structure sidesteps painful circular import errors.

Quick Check

Which call actually makes a blueprint active?

Recap

Define a Blueprint with a name and __name__, add routes with @bp.route, then call register_blueprint on the app to make them live. 🎉

Preguntas frecuentes

¿La lección «Cree y registre un Blueprint» es gratis?

Sí — el texto completo de «Cree y registre un Blueprint» 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 «Cree y registre un Blueprint»?

Defina un Blueprint y asígnelo a 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 2 de 4.

¿Cuánto tiempo toma la lección «Cree y registre un Blueprint»?

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. Por qué los Blueprints superan a un archivo gigante
  2. Cree y registre un Blueprint
  3. Prefijos de URL y url_for en Blueprints
  4. Plantillas y archivos estáticos específicos del Blueprint
← Volver a Flask Academy