render_template und der templates-Ordner
Geben Sie eine HTML-Datei statt eines Strings zurück.
render_template und der templates-Ordner ist eine kostenlose Flask Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Flask Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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! 🎉
Häufig gestellte Fragen
Ist die Lektion „render_template und der templates-Ordner“ kostenlos?
Ja — der vollständige Text von „render_template und der templates-Ordner“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Flask Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „render_template und der templates-Ordner“?
Geben Sie eine HTML-Datei statt eines Strings zurück. Du übst Flask Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Flask Academy zu starten?
Keine Vorkenntnisse erforderlich. Flask Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „render_template und der templates-Ordner“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Flask Academy-Lektion Code schreiben und ausführen?
Ja. Jede Flask Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- render_template und der templates-Ordner
- Daten an ein Template übergeben
- Jinja2-Schleifen und Bedingungen
- Autoescaping und der safe-Filter