0
0
Djangoframework~5 mins

Form validation (is_valid, cleaned_data) in Django - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the is_valid() method do in a Django form?
The is_valid() method checks if the form data meets all validation rules. It returns True if the data is valid, otherwise False. It also prepares cleaned data for use.
Click to reveal answer
beginner
What is cleaned_data in Django forms?
cleaned_data is a dictionary that holds the validated and converted form data after is_valid() is called. It contains safe data you can use in your code.
Click to reveal answer
beginner
When should you access cleaned_data in a Django form?
You should access cleaned_data only after is_valid() returns True. Accessing it before validation may cause errors.
Click to reveal answer
intermediate
How can you add custom validation logic in a Django form?
You can add a method named clean_fieldname() for a specific field or override the clean() method for the whole form to add custom validation.
Click to reveal answer
beginner
What happens if is_valid() returns False?
If is_valid() returns False, the form has errors. You can check form.errors to see what went wrong and show messages to the user.
Click to reveal answer
What does form.is_valid() return if the form data is correct?
ATrue
BFalse
CNone
DAn error message
Where do you find the cleaned and safe form data after validation?
Aform.errors
Bform.cleaned_data
Cform.raw_data
Dform.data
When should you NOT access form.cleaned_data?
AInside <code>clean()</code> method
BAfter <code>is_valid()</code> returns True
CBefore calling <code>is_valid()</code>
DWhen form is submitted
How do you add validation for a single field in a Django form?
ACall <code>is_valid()</code> twice
BOverride <code>save()</code> method
CUse <code>form.errors</code>
DDefine a method <code>clean_fieldname()</code>
What attribute contains error messages if validation fails?
Aform.errors
Bform.cleaned_data
Cform.is_valid
Dform.data
Explain how is_valid() and cleaned_data work together in Django form validation.
Think about the order of validation and data access.
You got /4 concepts.
    Describe how to add custom validation to a specific field in a Django form.
    Focus on naming and purpose of the method.
    You got /4 concepts.