Prefiksy URL i url_for w Blueprintach
Porządkuj przestrzenie nazw tras i twórz odnośniki między Blueprintami.
Prefiksy URL i url_for w Blueprintach to bezpłatna lekcja Flask Academy na CoddyKit. To lekcja 3 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.
Group URLs Under a Path
Often every route in a blueprint should sit under one path, like /admin. A url_prefix applies that path to the whole blueprint at once.
Set It at Registration
You pass the prefix when you register the blueprint. Every route inside then lives beneath that shared base path.
app.register_blueprint(admin, url_prefix="/admin")How Prefixes Combine
A route defined as /users under the prefix /admin becomes /admin/users. Flask simply joins the prefix and the route path.
Keep Routes Prefix-Free
Inside the blueprint, write routes without repeating the prefix. The prefix is set once at registration, so your view paths stay short.
@admin.route("/users")
def users():
return "Admin users"Stop Hardcoding Links
Never type URLs as plain strings in templates. url_for builds the correct link from an endpoint name, so links survive path changes.
Endpoints Are Namespaced
With blueprints, an endpoint is blueprint name plus view name. The users view in admin is the endpoint admin.users.
url_for("admin.users")Prefix Is Applied Automatically
url_for already knows the prefix. Calling it for admin.users returns /admin/users, prefix included, with zero extra work.
Linking Within the Same Blueprint
Inside one blueprint you can use a leading dot as a shortcut. url_for(".users") means the users view in the current blueprint.
url_for(".users")Linking Across Blueprints
To link from blog to admin, name both parts fully. Always spell out the blueprint when crossing module boundaries.
url_for("admin.dashboard")Pass Values to Dynamic Routes
Extra keyword arguments to url_for fill dynamic URL parts, so generated links stay correct even for parameterized routes.
url_for("blog.post", post_id=7)Refactor With Confidence
Change a prefix later and every url_for link updates itself. This is why url_for beats hardcoded strings every single time. 🔗
Quick Check
How do you link to a view inside a blueprint?
Recap
A url_prefix mounts every blueprint route under one path, and url_for builds namespaced links like admin.users that update automatically. 🎉
Często zadawane pytania
Czy lekcja „Prefiksy URL i url_for w Blueprintach” jest bezpłatna?
Tak — pełny tekst „Prefiksy URL i url_for w Blueprintach” 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 „Prefiksy URL i url_for w Blueprintach”?
Porządkuj przestrzenie nazw tras i twórz odnośniki między Blueprintami. Ć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 3 z 4.
Ile czasu zajmuje lekcja „Prefiksy URL i url_for w Blueprintach”?
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
- Dlaczego Blueprinty są lepsze niż jeden wielki plik
- Tworzenie i rejestrowanie Blueprintu
- Prefiksy URL i url_for w Blueprintach
- Szablony i pliki statyczne przypisane do Blueprintu