render_template i folder templates
Zwracaj plik HTML zamiast ciągu znaków.
render_template i folder templates 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.
From Strings to Pages
So far your routes returned plain strings. Real pages need HTML, and Flask gives you render_template to serve full HTML files cleanly.
What render_template Does
render_template finds an HTML file, runs it through the template engine, and returns the finished page as your response body.
from flask import render_template
@app.route('/')
def home():
return render_template('index.html')The templates Folder
Flask looks for your HTML in a folder named templates next to your app file. Put index.html there and Flask finds it automatically. 📁
myapp/
app.py
templates/
index.htmlWhy a Fixed Name
The name templates is a convention, not a guess. Flask knows where to look, so you never write the folder path inside render_template.
Reference Only the Filename
You pass just the filename, not a full path. Flask joins it onto the templates folder for you, keeping your code short and portable.
return render_template('about.html')Subfolders Inside templates
You can group pages in subfolders and reference them with forward slashes. This keeps a large project tidy as it grows.
return render_template('users/profile.html')A Minimal Template
A template is ordinary HTML at heart. The simplest one is just a regular page with no special syntax yet.
<!doctype html>
<title>Home</title>
<h1>Welcome</h1>Templates Get Rendered
Even a plain file is rendered: Flask processes it through Jinja2 first. That step is what lets you add dynamic pieces later.
Returning the Result
render_template returns a string of HTML, which you return from your view. Flask wraps it in a proper response with the right content type.
@app.route('/')
def home():
return render_template('index.html')TemplateNotFound Errors
If Flask cannot find the file, you get a TemplateNotFound error. Check the spelling and that the file truly sits inside templates.
Separating Logic and Markup
Templates keep HTML out of your Python. Views handle logic, templates handle presentation, and that split keeps both far easier to read.
Quick Check
Where does Flask expect your HTML files to live by default?
Recap
You learned that render_template serves HTML from the templates folder by filename, so logic stays in Python and markup stays in files. Nice work! 🎉
Często zadawane pytania
Czy lekcja „render_template i folder templates” jest bezpłatna?
Tak — pełny tekst „render_template i folder templates” 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 „render_template i folder templates”?
Zwracaj plik HTML zamiast ciągu znakó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 1 z 4.
Ile czasu zajmuje lekcja „render_template i folder templates”?
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