0Pricing
Flask Academy · Lekcja

Tworzenie i rejestrowanie Blueprintu

Zdefiniuj Blueprint i dołącz go do aplikacji.

Tworzenie i rejestrowanie Blueprintu to bezpłatna lekcja Flask Academy na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Flask Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Flask Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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. 🎉

Często zadawane pytania

Czy lekcja „Tworzenie i rejestrowanie Blueprintu” jest bezpłatna?

Tak — pełny tekst „Tworzenie i rejestrowanie Blueprintu” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Flask Academy, przejdź na CoddyKit PRO. Kurs Flask Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „Tworzenie i rejestrowanie Blueprintu”?

Zdefiniuj Blueprint i dołącz go do aplikacji. Ćwiczysz Flask Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Flask Academy?

Nie wymagamy żadnego doświadczenia. Flask Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.

Ile czasu zajmuje lekcja „Tworzenie i rejestrowanie Blueprintu”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Flask Academy?

Tak. Każda lekcja Flask Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Dlaczego Blueprinty są lepsze niż jeden wielki plik
  2. Tworzenie i rejestrowanie Blueprintu
  3. Prefiksy URL i url_for w Blueprintach
  4. Szablony i pliki statyczne przypisane do Blueprintu
← Powrót do Flask Academy