0Pricing
Django Academy · Lezione

Definire un forms.Form

Dichiari i campi e i relativi widget

Definire un forms.Form è una lezione Django Academy gratuita su CoddyKit. Questa è la lezione 1 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.

Why Forms Exist

A Django form turns messy user input into clean Python data, handling rendering and validation in one tidy place. 📝

Subclass forms.Form

You declare a form by subclassing forms.Form. Each class attribute you add becomes a field on that form.

from django import forms

class ContactForm(forms.Form):
    name = forms.CharField()

Fields Are Class Attributes

Every field is a class attribute set to a field instance, like CharField or EmailField. The attribute name becomes the input name.

name = forms.CharField()
email = forms.EmailField()

CharField for Text

Use CharField for short text. Set max_length to cap the size and render a sensible HTML input automatically.

subject = forms.CharField(max_length=120)

EmailField Validates Format

An EmailField checks that the value looks like a real email address, so you do not write that validation yourself.

email = forms.EmailField()

Required by Default

Every field is required unless you say otherwise. Pass required=False to make a field optional for the user.

phone = forms.CharField(required=False)

label and help_text

Add a friendly label and help_text so users understand each field. Django shows them next to the input.

name = forms.CharField(label="Your name",
    help_text="First and last")

Choosing a Widget

A widget controls the HTML rendered for a field. Swap the default to get a textarea, password box, or select.

message = forms.CharField(
    widget=forms.Textarea)

More Field Types

Django ships many fields: IntegerField, BooleanField, DateField, and ChoiceField each map to the right input and parse the value for you.

age = forms.IntegerField()
agree = forms.BooleanField()

ChoiceField and Options

A ChoiceField renders a dropdown from a list of value and label pairs you provide in choices.

plan = forms.ChoiceField(choices=[
    ("free", "Free"), ("pro", "Pro")])

Instantiate the Form

Create an unbound form with no data to display a blank form, or pass data to bind it for validation.

form = ContactForm()  # blank
form = ContactForm(request.POST)

Quick Check

Let us see how forms are defined.

Recap

You subclass forms.Form and add fields as class attributes. Pick the right field type and widget, and Django handles the rest. ✅

Domande Frequenti

La lezione «Definire un forms.Form» è gratuita?

Sì — il testo completo di «Definire un forms.Form» è 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 «Definire un forms.Form»?

Dichiari i campi e i relativi widget 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 1 di 4.

Quanto tempo richiede la lezione «Definire un forms.Form»?

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

  1. Definire un forms.Form
  2. is_valid e cleaned_data
  3. Visualizzare i form nei template
  4. Protezione CSRF e flusso POST
← Torna a Django Academy