Recall & Review
beginner
What is the purpose of custom form validation methods in Django?
Custom form validation methods let you check user input beyond basic rules. They help ensure data is correct and meaningful before saving or processing.Click to reveal answer
beginner
How do you name a method to validate a specific field in a Django form?
Name the method
clean_fieldname, replacing fieldname with your form field's name. Django calls it automatically to validate that field.Click to reveal answer
beginner
What should a
clean_fieldname method return in Django?It should return the cleaned (validated and possibly modified) value for that field. If invalid, it should raise
ValidationError.Click to reveal answer
intermediate
What is the difference between
clean_fieldname and clean() methods in Django forms?clean_fieldname validates one field at a time. clean() validates the whole form and can check multiple fields together.Click to reveal answer
beginner
How do you raise a validation error inside a custom validation method?
Use <code>from django.core.exceptions import ValidationError</code> and then raise <code>ValidationError('Your error message')</code> when validation fails.Click to reveal answer
Which method name will Django call automatically to validate a form field named 'email'?
✗ Incorrect
Django calls methods named clean_ automatically for field validation.
What should a custom field validation method return if the input is valid?
✗ Incorrect
The method must return the cleaned (possibly modified) value for the field.
Where do you put validation logic that depends on multiple fields in a Django form?
✗ Incorrect
The clean() method validates the whole form and can access multiple fields.
What happens if you raise ValidationError inside a custom validation method?
✗ Incorrect
Raising ValidationError stops form validation and shows the error to the user.
Which import is needed to raise a ValidationError in Django forms?
✗ Incorrect
ValidationError is imported from django.core.exceptions.
Explain how to create a custom validation method for a single field in a Django form.
Think about method naming and what it returns or raises.
You got /3 concepts.
Describe when and why you would use the clean() method in a Django form.
Consider validation that depends on more than one field.
You got /3 concepts.