0
0
Djangoframework~10 mins

Form class definition in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Form class definition
Define Form class
Add fields as class attributes
Set field types and options
Use form in views/templates
Render form and handle input
This flow shows how to create a Django form class by defining fields, then use it to render and process user input.
Execution Sample
Django
from django import forms

class ContactForm(forms.Form):
    name = forms.CharField(max_length=100)
    email = forms.EmailField()
    message = forms.CharField(widget=forms.Textarea)
Defines a simple contact form with name, email, and message fields.
Execution Table
StepActionEvaluationResult
1Import forms modulefrom django import formsforms module ready
2Define ContactForm classclass ContactForm(forms.Form):ContactForm class created
3Add 'name' fieldname = forms.CharField(max_length=100)name field added with max_length=100
4Add 'email' fieldemail = forms.EmailField()email field added with email validation
5Add 'message' fieldmessage = forms.CharField(widget=forms.Textarea)message field added with textarea widget
6Use form in view/templateform = ContactForm()form instance created for rendering/input
7Render form in templateform.as_p()HTML form rendered with fields
8Handle form submissionform = ContactForm(request.POST)form instance with submitted data
9Validate formform.is_valid()True if data valid, else False
10Process valid dataform.cleaned_dataAccess cleaned input values
11ExitForm class definition and usage completeEnd of flow
💡 All form fields defined and form usage steps completed
Variable Tracker
VariableStartAfter Step 6After Step 8After Step 9Final
ContactFormNot definedClass definedClass definedClass definedClass defined
formNoneInstance with empty fieldsInstance with POST dataValidated form instanceValidated form instance
form.cleaned_dataNoneNoneNoneDictionary of cleaned dataDictionary of cleaned data
Key Moments - 3 Insights
Why do we define fields as class attributes inside the form class?
Fields are defined as class attributes so Django can automatically create form inputs and validations, as shown in execution_table steps 3-5.
What happens when we call form.is_valid()?
Calling form.is_valid() runs validation on all fields and sets cleaned_data if valid, as shown in execution_table step 9.
How does the form class relate to the HTML form rendered?
The form class defines the structure and validation; calling form.as_p() renders HTML inputs matching those fields, as in step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of 'form' after step 6?
AAn instance with submitted POST data
BAn instance with empty fields ready for rendering
CA class definition, not an instance
DA dictionary of cleaned data
💡 Hint
Check the 'form' variable in variable_tracker after step 6
At which step does the form validate the submitted data?
AStep 9
BStep 4
CStep 7
DStep 11
💡 Hint
Look for form.is_valid() in execution_table
If we add a new field 'phone = forms.CharField()', which step would change?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Adding a new field is similar to adding 'message' field in step 5
Concept Snapshot
Define a Django form by creating a class inheriting from forms.Form
Add fields as class attributes with field types and options
Use form instances in views to render and process input
Call form.is_valid() to validate and access cleaned_data
Rendering uses methods like form.as_p() for HTML output
Full Transcript
In Django, you create a form by defining a class that inherits from forms.Form. Inside this class, you add fields as class attributes, specifying their types like CharField or EmailField. This tells Django what inputs to create and how to validate them. When you use the form in your view, you create an instance of this class. You can render it in your template to show the form to users. When users submit data, you create a form instance with that data and call is_valid() to check if the input meets all rules. If valid, you access cleaned_data to get safe input values. This process helps you handle user input easily and safely.