0
0
Djangoframework~20 mins

Form class definition in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Django Form Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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
Avalid is True, errors is an empty dictionary
Bvalid is True, errors contains 'email' key
Cvalid is False, errors contains 'name' key
Dvalid is False, errors contains 'email' key
Attempts:
2 left
💡 Hint
Check if the provided email matches the EmailField format and if all required fields are present.
📝 Syntax
intermediate
2: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.
A
class AgeForm(forms.Form):
    age = forms.IntegerField(required=False)
B
class AgeForm(forms.Form):
    age = forms.IntegerField(required=True)
C
class AgeForm(forms.Form):
    age = forms.CharField(required=True)
D
class AgeForm(forms.Form):
    age = forms.FloatField(required=True)
Attempts:
2 left
💡 Hint
Look for the field type that accepts only integers and is required.
state_output
advanced
2: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
A{'username': 'bob', 'age': 25}
B{'username': 'bob', 'age': '25'}
C{'username': 'bob'}
DRaises ValidationError
Attempts:
2 left
💡 Hint
Remember that IntegerField converts input strings to integers in cleaned_data.
🔧 Debug
advanced
2: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')
ABecause forms.Form cannot be instantiated with data argument
BBecause CharField requires an integer max_length
CBecause data must be a dictionary or QueryDict, not a string
DBecause bio field is missing a required argument
Attempts:
2 left
💡 Hint
Check the type expected for the data parameter when creating a form instance.
🧠 Conceptual
expert
2:00remaining
Which statement about Django form class definition is true?
Select the correct statement about defining and using Django form classes.
AForm classes cannot have custom validation methods
BForm fields can only be defined inside the __init__ method of the form class
CDjango forms automatically save data to the database when is_valid() is called
DA form class must inherit from forms.Form and define fields as class attributes
Attempts:
2 left
💡 Hint
Think about how Django expects form fields to be declared.