Registre Blueprints na fábrica
Conecte todos os Blueprints durante a montagem do aplicativo.
Registre Blueprints na fábrica é uma aula grátis de Flask Academy no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Flask Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Flask Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em 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 appImport 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. 🏁
Perguntas Frequentes
A aula “Registre Blueprints na fábrica” é grátis?
Sim — o texto completo de “Registre Blueprints na fábrica” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Flask Academy, atualize para CoddyKit PRO. O curso de Flask Academy inclui 4 aulas no total.
O que vou aprender em “Registre Blueprints na fábrica”?
Conecte todos os Blueprints durante a montagem do aplicativo. Você pratica Flask Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Flask Academy?
Nenhuma experiência prévia é necessária. Flask Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “Registre Blueprints na fábrica”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Flask Academy?
Sim. Cada aula de Flask Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Problemas com um objeto app global
- Escreva uma função create_app
- Inicialize extensões com init_app
- Registre Blueprints na fábrica