Challenge - 5 Problems
Form Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Django form validation?
Consider this Django form code snippet. What will form.is_valid() return if the input data is {'name': '', 'age': '25'}?
Django
from django import forms class PersonForm(forms.Form): name = forms.CharField(required=True) age = forms.IntegerField(required=True) form = PersonForm({'name': '', 'age': '25'}) valid = form.is_valid()
Attempts:
2 left
💡 Hint
Think about what happens when a required CharField is empty.
✗ Incorrect
The 'name' field is required and empty, so form.is_valid() returns false.
❓ state_output
intermediate2:00remaining
What error message does this form produce for invalid email?
Given this Django form, what error message will appear if the email input is 'invalid-email'?
Django
from django import forms class EmailForm(forms.Form): email = forms.EmailField() form = EmailForm({'email': 'invalid-email'}) form.is_valid() errors = form.errors['email']
Attempts:
2 left
💡 Hint
Django's EmailField has a built-in validator with a standard message.
✗ Incorrect
Django's EmailField uses the message 'Enter a valid email address.' for invalid emails.
📝 Syntax
advanced2:30remaining
Which option correctly overrides the clean method in a Django form?
You want to add custom validation to a Django form by overriding the clean method. Which code snippet is correct?
Django
from django import forms class MyForm(forms.Form): age = forms.IntegerField() def clean(self): # custom validation here pass
Attempts:
2 left
💡 Hint
Remember to call super().clean() to get cleaned_data and return it.
✗ Incorrect
Option D correctly calls super().clean(), checks age, raises ValidationError, and returns cleaned_data.
🔧 Debug
advanced2:30remaining
Why does this form validation always fail?
This Django form always fails validation even with valid data. What is the cause?
Django
from django import forms class SampleForm(forms.Form): name = forms.CharField() def clean_name(self): name = self.cleaned_data.get('name') if not name.isalpha(): raise forms.ValidationError('Only letters allowed.') # Missing return statement here form = SampleForm({'name': 'Alice'}) valid = form.is_valid()
Attempts:
2 left
💡 Hint
Check the clean_name method's return behavior.
✗ Incorrect
clean_() methods must return the cleaned value; missing return causes validation failure.
🧠 Conceptual
expert3:00remaining
What is the effect of calling form.full_clean() twice in Django?
In Django forms, what happens if you call form.full_clean() two times in a row on the same form instance?
Django
from django import forms class TestForm(forms.Form): field = forms.CharField() form = TestForm({'field': 'value'}) form.full_clean() form.full_clean()
Attempts:
2 left
💡 Hint
Consider what full_clean does internally each time it runs.
✗ Incorrect
full_clean resets errors and cleaned_data each time it runs, so calling it twice cleans the form twice.