HTML-Templates
Struktur
app.py
templates/
home.html
app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.get("/")
def home():
return render_template("home.html", name="Luna", punkte=12)
templates/home.html
<!doctype html>
<html lang="de">
<head>
<meta charset="utf-8" />
<title>Code-Club</title>
</head>
<body>
<h1>Hallo {{ name }}!</h1>
<p>Punkte: {{ punkte }}</p>
</body>
</html>
{{ ... }} setzt Werte ein.
Rate mal!
Liegt HTML neben app.py lose?
Auflösung
Standard: im Ordner templates/.
Probiere es selbst
Experiment 1
Template mit Namen.
Experiment 2
Zweite Variable.
Experiment 3
Liste im Template (for-Schleife Jinja – optional).
Übungen
Level 1
home.html mit h1.
Level 2
Variable übergeben.
Level 3
Seite /stats mit zwei Zahlen.
Level-2-Lösung
Siehe Beispiel home + render_template.
Mini-Quiz
Templates sitzen! Als Nächstes: Formulare und JSON-Routen.