Challenge - 5 Problems
Django Form Mastery
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?
Given the following Django form class and data, what will be the value of
form.is_valid() and form.errors after validation?Django
from django import forms class ContactForm(forms.Form): name = forms.CharField(max_length=50) email = forms.EmailField() form = ContactForm(data={'name': 'Alice', 'email': 'alice@example.com'}) valid = form.is_valid() errors = form.errors
Attempts:
2 left
💡 Hint
Check if the provided email matches the EmailField format and if all required fields are present.
✗ Incorrect
The form has both required fields filled correctly. The email is valid, so
form.is_valid() returns True and form.errors is empty.📝 Syntax
intermediate2:00remaining
Which option correctly defines a Django form with a required integer field?
Select the code snippet that correctly defines a Django form class with a required integer field named
age.Attempts:
2 left
💡 Hint
Look for the field type that accepts only integers and is required.
✗ Incorrect
Option B uses IntegerField with required=True, which matches the requirement exactly. Option B makes it optional. Option B uses CharField, which accepts strings. Option B uses FloatField, which accepts floats.
❓ state_output
advanced2:00remaining
What is the cleaned_data content after form validation?
Given this Django form and input data, what will
form.cleaned_data contain after calling form.is_valid()?Django
from django import forms class SignupForm(forms.Form): username = forms.CharField(max_length=30) age = forms.IntegerField() form = SignupForm(data={'username': 'bob', 'age': '25'}) form.is_valid() cleaned = form.cleaned_data
Attempts:
2 left
💡 Hint
Remember that IntegerField converts input strings to integers in cleaned_data.
✗ Incorrect
The form converts the 'age' string '25' to integer 25 in cleaned_data after validation.
🔧 Debug
advanced2:00remaining
Why does this Django form raise a TypeError?
Examine the following form class. Why does instantiating it with data cause a TypeError?
Django
from django import forms class ProfileForm(forms.Form): bio = forms.CharField(max_length=100) form = ProfileForm(data='not a dict')
Attempts:
2 left
💡 Hint
Check the type expected for the data parameter when creating a form instance.
✗ Incorrect
The data argument must be a dictionary-like object. Passing a string causes a TypeError.
🧠 Conceptual
expert2:00remaining
Which statement about Django form class definition is true?
Select the correct statement about defining and using Django form classes.
Attempts:
2 left
💡 Hint
Think about how Django expects form fields to be declared.
✗ Incorrect
Django form classes inherit from forms.Form and define fields as class attributes. Custom validation methods are allowed. Forms do not save data automatically.