Visualizzare i form nei template
Mostri campi, etichette ed errori in modo sicuro
Visualizzare i form nei template è una lezione Django Academy gratuita su CoddyKit. Questa è la lezione 3 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 Django Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Django Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Pass the Form In
Send your form to the template through the context, just like any other variable, so it can be rendered.
return render(request, "contact.html",
{"form": form})Render Everything at Once
Printing {{ form }} outputs every field with its label, widget, and errors. It is the fastest way to get a working form.
<form method="post">
{{ form }}
</form>Pick a Layout Helper
Use as_p, as_div, or as_table to wrap each field in a tidy structure without writing the markup yourself.
{{ form.as_p }}Render One Field
Access a single field by name to control its placement. {{ form.name }} renders just that widget.
{{ form.email }}Show the Label
Pair a field with label_tag to output a proper label element linked to the input for accessibility.
{{ form.email.label_tag }}
{{ form.email }}Display Field Errors
Each bound field exposes its own errors, so you can place validation messages right beside the matching input.
{{ form.email.errors }}Non-Field Errors
Errors raised in form-wide clean() are non_field_errors. Render them at the top of the form, not on any single field.
{{ form.non_field_errors }}Loop Over Fields
Iterate the form with a for loop to render every field the same way while keeping full control of the wrapper markup.
{% for field in form %}
{{ field.label_tag }} {{ field }}
{% endfor %}Show help_text
Inside the loop, output field.help_text to guide users with the hint you defined on that field.
{{ field.help_text }}Add the Submit Button
Django renders inputs but not the submit button or the form tag, so you always add those yourself.
<button type="submit">Send</button>Auto-Escaping Keeps You Safe
The template engine escapes rendered values by default, so user input cannot inject HTML and cause XSS.
Quick Check
How do forms render in templates?
Recap
Pass the form in, render it with as_p or field by field, and show errors. You always add the form tag and button. ✅
Domande Frequenti
La lezione «Visualizzare i form nei template» è gratuita?
Sì — il testo completo di «Visualizzare i form nei 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 Django Academy, passa a CoddyKit PRO. Il corso Django Academy include 4 lezioni in totale.
Cosa imparerò in «Visualizzare i form nei template»?
Mostri campi, etichette ed errori in modo sicuro Eserciti Django 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 Django Academy?
Non è richiesta alcuna esperienza precedente. Django 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 3 di 4.
Quanto tempo richiede la lezione «Visualizzare i form nei 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 Django Academy?
Sì. Ogni lezione Django 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
- Definire un forms.Form
- is_valid e cleaned_data
- Visualizzare i form nei template
- Protezione CSRF e flusso POST