is_valid and cleaned_data
Validate input and read clean values.
is_valid and cleaned_data is a free Django Academy lesson on CoddyKit — lesson 2 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.
Bind Data First
To validate, create a bound form by passing the submitted data into it. Only bound forms can be validated.
form = ContactForm(request.POST)Call is_valid()
is_valid() runs every field validation and returns True only if all of them pass. It triggers the whole cleaning process.
if form.is_valid():
handle(form)cleaned_data Appears
After is_valid() returns True, Django fills cleaned_data, a dict of validated, type-converted values ready to use.
name = form.cleaned_data["name"]Values Are Converted
Cleaning converts types too. An IntegerField gives you a real int, and a DateField gives you a Python date, not raw strings.
age = form.cleaned_data["age"] # intErrors Collect Automatically
If validation fails, the messages land in form.errors, keyed by field name so you can show each problem clearly.
if not form.is_valid():
print(form.errors)Field-Level clean Methods
Add a method named clean_field to validate one field and return its cleaned value. Raise ValidationError to reject it.
def clean_name(self):
name = self.cleaned_data["name"]
return name.strip()Raise ValidationError
Inside a clean method, raise ValidationError with a message to reject the value and attach an error to that field.
if "@" not in email:
raise forms.ValidationError("Bad email")Form-Wide clean()
Override clean() to validate fields against each other, like confirming two passwords match across the whole form.
def clean(self):
cleaned = super().clean()
return cleanedCross-Field Checks
In clean() you can read several cleaned_data keys and raise an error when their combination is invalid.
if start > end:
raise forms.ValidationError("Bad range")The Cleaning Order
Django runs field cleaning first, then your form-wide clean(). So clean() can safely rely on already-validated field values.
Use cleaned_data Safely
Always read input from cleaned_data, never from request.POST. Only cleaned_data holds validated, converted, trusted values.
email = form.cleaned_data["email"]Quick Check
Test your grip on validation.
Recap
Bind data, call is_valid(), then read trusted values from cleaned_data. Use clean methods to add your own rules. ✅
Frequently asked questions
Is the “is_valid and cleaned_data” lesson free?
Yes — the full text of “is_valid and cleaned_data” 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 “is_valid and cleaned_data”?
Validate input and read clean values. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “is_valid and cleaned_data” 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.
All lessons in this course
- Defining a forms.Form
- is_valid and cleaned_data
- Rendering Forms in Templates
- CSRF Protection and the POST Flow