Konwertery typów: int, float, string, path
Ograniczaj segmenty URL za pomocą wbudowanych konwerterów.
Konwertery typów: int, float, string, path 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.
Strings Are Not Always Enough
Captured values default to strings, but /post/42 should give a number, not text. A converter tells Flask what type to expect in that segment.
Converter Syntax
Put the converter name before the variable with a colon. The pattern is <converter:name>, and Flask applies it before calling your view.
@app.route('/post/<int:post_id>')
def show(post_id):
return str(post_id + 1)The int Converter
Use int to capture whole numbers. /post/42 hands your view the integer 42, so you can do math without calling int() yourself. 🔢
@app.route('/page/<int:n>')
def page(n):
return f'Page {n}'int Rejects Non-Numbers
A converter also filters URLs. /page/abc will not match an int route, so Flask returns 404 instead of crashing inside your function.
The float Converter
Use float for decimal numbers like prices or ratings. /rate/4.5 gives you the float 4.5, ready for arithmetic right away.
@app.route('/rate/<float:score>')
def rate(score):
return str(score * 2)The string Converter
string is the default: it accepts any text but stops at a slash. You rarely write it explicitly since plain <name> already behaves this way.
@app.route('/tag/<string:label>')
def tag(label):
return labelThe path Converter
Use path when the value can contain slashes, like a folder route. Unlike string, it captures /docs/intro/setup as one whole value.
@app.route('/files/<path:filepath>')
def serve(filepath):
return filepathPick the Right Tool
Match the converter to your data: int for ids, float for decimals, string for words, path for slashed routes. The right choice prevents bad input early.
Converters Validate for You
By rejecting mismatched URLs with a 404, converters act as a first line of validation. Your view only runs when the type already fits.
Combine in One Route
You can mix converters across segments. Here an int id and a string tab live in the same path, each typed independently.
@app.route('/user/<int:uid>/<tab>')
def view(uid, tab):
return f'{uid}:{tab}'Negatives and Edge Cases
The built-in int converter matches only non-negative whole numbers by default. For minus signs or stricter rules, you would write a custom converter later.
Quick Check
Which converter lets a captured value include slashes, like docs/intro/setup?
Recap: You Typed Your URLs
Converters give captured values a type and reject bad input with a 404. Remember int, float, string, and path. Next: building URLs safely. 🧭
Często zadawane pytania
Czy lekcja „Konwertery typów: int, float, string, path” jest bezpłatna?
Tak — pełny tekst „Konwertery typów: int, float, string, path” 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 „Konwertery typów: int, float, string, path”?
Ograniczaj segmenty URL za pomocą wbudowanych konwerterów. Ć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 „Konwertery typów: int, float, string, path”?
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