0
0
Djangoframework~20 mins

Form validation (is_valid, cleaned_data) in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Form Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does form.is_valid() do in Django forms?
Consider a Django form instance with user input data. What is the main purpose of calling form.is_valid()?
AIt converts the form data into JSON format.
BIt saves the form data directly to the database without validation.
CIt clears all the data from the form fields.
DIt checks if the form data meets all field validations and returns True or False accordingly.
Attempts:
2 left
💡 Hint
Think about what happens before you use the form data safely.
state_output
intermediate
2:00remaining
What is the content of form.cleaned_data after validation?
After calling form.is_valid() and it returns True, what does form.cleaned_data contain?
Django
from django import forms

class SampleForm(forms.Form):
    name = forms.CharField(max_length=10)
    age = forms.IntegerField()

form = SampleForm({'name': 'Alice', 'age': '30'})
if form.is_valid():
    data = form.cleaned_data
else:
    data = None
AA dictionary with form fields as keys and validated, converted Python values as values.
BA list of error messages for each invalid field.
CThe original input dictionary without any changes.
DA string representation of the form HTML.
Attempts:
2 left
💡 Hint
Think about what you want to use after validation passes.
📝 Syntax
advanced
2:30remaining
Which option correctly defines a custom clean method in a Django form?
You want to add a custom validation that checks if the 'age' field is at least 18. Which code snippet correctly implements this in a Django form?
A
def clean_age(self):
    age = self.data['age']
    if age < 18:
        raise forms.ValidationError('Must be at least 18')
    return age
B
def clean(self):
    age = self.cleaned_data['age']
    if age < 18:
        raise forms.ValidationError('Must be at least 18')
    return self.cleaned_data
C
def clean_age(self):
    age = self.cleaned_data.get('age')
    if age < 18:
        raise forms.ValidationError('Must be at least 18')
    return age
D
def clean(self):
    age = self.data.get('age')
    if age < 18:
        raise forms.ValidationError('Must be at least 18')
    return self.cleaned_data
Attempts:
2 left
💡 Hint
Custom field validation uses clean_fieldname methods and accesses cleaned_data.
🔧 Debug
advanced
2:00remaining
Why does this form always return False for is_valid()?
Given this form code, why does form.is_valid() always return False?
class MyForm(forms.Form):
    email = forms.EmailField()

form = MyForm({'email': 'not-an-email'})
valid = form.is_valid()
AThe email value is not a valid email format, so validation fails.
BThe form is missing a required field called 'email'.
CThe form data dictionary keys must be bytes, not strings.
DThe form instance was not created with a request object.
Attempts:
2 left
💡 Hint
Check the format of the input data for the EmailField.
🧠 Conceptual
expert
2:30remaining
What happens if you access form.cleaned_data before calling form.is_valid()?
Consider this code snippet:
form = MyForm({'name': 'Bob'})
data = form.cleaned_data
What will happen when you try to access form.cleaned_data before calling form.is_valid()?
AIt returns an empty dictionary because no validation has run yet.
BIt raises an AttributeError because cleaned_data is not set until is_valid() is called.
CIt returns the raw input data dictionary passed to the form.
DIt automatically calls is_valid() and returns cleaned data.
Attempts:
2 left
💡 Hint
Think about when cleaned_data is created in the form lifecycle.