Defining a forms.Form
Declare fields and their widgets.
Defining a forms.Form is a free Django Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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. ✅
Frequently asked questions
Is the “Defining a forms.Form” lesson free?
Yes — the full text of “Defining a forms.Form” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.
What will I learn in “Defining a forms.Form”?
Declare fields and their widgets. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Django Academy?
No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Defining a forms.Form” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Django Academy lesson?
Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.