Passare dati a un template
Invii variabili Python alla pagina renderizzata.
Passare dati a un template è una lezione Flask Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Flask Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Flask Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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! 🚀
Domande Frequenti
La lezione «Passare dati a un template» è gratuita?
Sì — il testo completo di «Passare dati a un template» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Flask Academy, passa a CoddyKit PRO. Il corso Flask Academy include 4 lezioni in totale.
Cosa imparerò in «Passare dati a un template»?
Invii variabili Python alla pagina renderizzata. Eserciti Flask Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Flask Academy?
Non è richiesta alcuna esperienza precedente. Flask Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «Passare dati a un template»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Flask Academy?
Sì. Ogni lezione Flask Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- render_template e la cartella templates
- Passare dati a un template
- Cicli e condizioni in Jinja2
- Autoescaping e filtro safe