Przekazywanie danych do szablonu
Przekazuj zmienne Pythona do wyrenderowanej strony.
Przekazywanie danych do szablonu 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.
Pages That Change
Static HTML is fine, but real pages show different data per user. You make pages dynamic by passing data from your view into the template.
Keyword Arguments
Add keyword arguments to render_template. Each name you pass becomes a variable you can use inside the HTML file.
return render_template('hi.html', name='Ada')Reading a Variable
Inside the template, print a value with double curly braces. Flask swaps the expression for its result when the page renders.
<h1>Hello, {{ name }}!</h1>The Expression Delimiter
Those {{ }} markers mean output this value here. Anything inside is evaluated and its result is written into the page.
Passing Many Values
You can pass as many keyword arguments as you need. Each one is independent and available throughout the template.
return render_template('p.html', name='Ada', age=36)Passing Lists
Values are not limited to text. You can hand a template a list or any Python object and work with it in the markup.
return render_template('t.html', items=['a','b','c'])Accessing Object Attributes
Use a dot to reach attributes, just like in Python. Templates read object properties the same intuitive way you do in code.
<p>{{ user.email }}</p>Dictionary Lookups
For a dictionary, use either the dot or bracket form. Jinja2 tries attribute access first, then falls back to a key lookup.
{{ data.title }} or {{ data['title'] }}Missing Values Are Safe
If a variable is undefined, Jinja2 renders nothing instead of crashing. That keeps a small typo from breaking the whole page.
Applying Filters
Transform a value on the fly with a filter, written after a pipe. Filters format text without cluttering your Python view.
<h1>{{ name|upper }}</h1>Keep Logic in the View
Do heavy work in Python and pass ready values in. Templates should display data, not compute it, which keeps pages simple.
Quick Check
How do you output a variable named title inside a Jinja2 template?
Recap
You now pass data through keyword arguments and print it with double curly braces, even reaching attributes and applying filters. Great progress! 🚀
Często zadawane pytania
Czy lekcja „Przekazywanie danych do szablonu” jest bezpłatna?
Tak — pełny tekst „Przekazywanie danych do szablonu” 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 „Przekazywanie danych do szablonu”?
Przekazuj zmienne Pythona do wyrenderowanej strony. Ć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 „Przekazywanie danych do szablonu”?
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
- render_template i folder templates
- Przekazywanie danych do szablonu
- Pętle i instrukcje warunkowe Jinja2
- Automatyczne escapowanie i filtr safe