Pobieranie zmiennych ze ścieżki
Używaj parametrów w nawiasach ostrych w trasach.
Pobieranie zmiennych ze ścieżki to bezpłatna lekcja Flask Academy na CoddyKit. To lekcja 1 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.
Static Routes Hit a Wall
A fixed path like /user/alice only works for one person. To serve any name, you need a route that captures part of the URL as data.
Angle Brackets Capture Values
Wrap a path segment in angle brackets to turn it into a variable. Flask grabs whatever the user types there and hands it to your view as a parameter. 🎯
@app.route('/user/<name>')
def profile(name):
return f'Hello, {name}!'The Name Must Match
The word inside the brackets becomes the function argument. The name in the URL and the parameter in your view function must be spelled identically.
@app.route('/city/<place>')
def show(place):
return placeValues Arrive as Strings
By default a captured value is always a Python string. Visiting /user/42 gives you the text "42", not the number 42.
@app.route('/id/<value>')
def show(value):
return type(value).__name__Capture More Than One
A single route can capture several segments. Each set of angle brackets becomes its own argument in the view function, in order.
@app.route('/<category>/<item>')
def show(category, item):
return f'{category}: {item}'Mix Fixed and Dynamic Parts
You can blend static text with captured pieces. Only the bracketed segment is dynamic; the rest of the path stays fixed and literal.
@app.route('/posts/<slug>/edit')
def edit(slug):
return slugUse the Value in Your Logic
Once captured, the variable is just normal Python. Look it up in a database, format it, or branch on it like any other value.
@app.route('/user/<name>')
def hi(name):
return f'Welcome back, {name.title()}'Each Request Is Independent
Every visit fills the captured variable fresh. One user hitting /user/sam and another hitting /user/lee get separate, isolated requests.
Slashes Split Segments
A normal capture stops at the next slash. So name in /user/<name> will not include any /, since the slash marks the end of that segment.
Naming Your Variables Well
Pick clear names that describe the data, like <username> or <product_id>. Good names make routes self-documenting and easier to maintain.
A Tiny Real Example
Captured variables power friendly URLs like /greet/Maria. The view simply uses the value to build a personalized response for each visitor.
@app.route('/greet/<who>')
def greet(who):
return f'Nice to meet you, {who}'Quick Check
You wrote /user/<name> with def profile(name). What gets passed to name?
Recap: You Captured the Path
Angle brackets turn part of a URL into a function argument. Captured values arrive as strings, and you can grab several at once. Next: typing them. 🚀
Często zadawane pytania
Czy lekcja „Pobieranie zmiennych ze ścieżki” jest bezpłatna?
Tak — pełny tekst „Pobieranie zmiennych ze ścieżki” 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 „Pobieranie zmiennych ze ścieżki”?
Używaj parametrów w nawiasach ostrych w trasach. Ć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 1 z 4.
Ile czasu zajmuje lekcja „Pobieranie zmiennych ze ścieżki”?
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
- Pobieranie zmiennych ze ścieżki
- Konwertery typów: int, float, string, path
- Budowanie URL-i za pomocą url_for
- Końcowe ukośniki i przekierowania