0
0
Djangoframework~20 mins

Why Django forms matter - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Django Forms Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a Django form is submitted with invalid data?
Consider a Django form that requires a user's email. What is the behavior when the form is submitted with an empty email field?
Django
from django import forms

class EmailForm(forms.Form):
    email = forms.EmailField()

form = EmailForm(data={'email': ''})
valid = form.is_valid()
errors = form.errors.get('email')
Avalid is False and errors contains a message about the email being required
Bvalid is True but errors contains a message about the email being required
Cvalid is False but errors is empty
Dvalid is True and errors is empty
Attempts:
2 left
💡 Hint
Think about what Django does when required fields are missing or invalid.
state_output
intermediate
2:00remaining
What is the cleaned_data content after form validation?
Given a Django form with a CharField named 'name', what does form.cleaned_data contain after calling form.is_valid() with valid input?
Django
from django import forms

class NameForm(forms.Form):
    name = forms.CharField()

form = NameForm(data={'name': 'Alice'})
form.is_valid()
result = form.cleaned_data
A{}
B{'name': 'Alice'}
C{'name': ''}
DRaises an AttributeError
Attempts:
2 left
💡 Hint
Look at what cleaned_data holds after validation passes.
📝 Syntax
advanced
2:30remaining
Which option correctly defines a Django form with a custom validation method?
You want to add a custom validation to a Django form field 'age' to ensure it is at least 18. Which code snippet is correct?
A
class AgeForm(forms.Form):
    age = forms.IntegerField()
    def clean_age(self):
        if self.age < 18:
            raise forms.ValidationError('Must be 18 or older')
B
class AgeForm(forms.Form):
    age = forms.IntegerField()
    def validate_age(self):
        if self.age < 18:
            raise forms.ValidationError('Must be 18 or older')
C
class AgeForm(forms.Form):
    age = forms.IntegerField()
    def clean(self):
        if self.cleaned_data['age'] < 18:
            raise forms.ValidationError('Must be 18 or older')
D
class AgeForm(forms.Form):
    age = forms.IntegerField()
    def clean_age(self):
        age = self.cleaned_data['age']
        if age < 18:
            raise forms.ValidationError('Must be 18 or older')
        return age
Attempts:
2 left
💡 Hint
Custom field validation methods must be named clean_ and return the cleaned value.
🔧 Debug
advanced
2:30remaining
Why does this Django form raise a KeyError during validation?
Examine this form code and identify why a KeyError occurs when calling form.is_valid():
Django
from django import forms

class ProductForm(forms.Form):
    name = forms.CharField()
    price = forms.DecimalField()

    def clean(self):
        if self.cleaned_data['price'] <= 0:
            raise forms.ValidationError('Price must be positive')
        return self.cleaned_data

form = ProductForm(data={'name': 'Book'})
form.is_valid()
ABecause 'price' is missing in data, accessing self.cleaned_data['price'] causes KeyError
BBecause 'name' is missing in data, causing KeyError in clean_price
CBecause DecimalField cannot handle string input
DBecause clean_price method does not return a value
Attempts:
2 left
💡 Hint
Check what happens if a required field is missing and you try to access it in cleaning.
🧠 Conceptual
expert
3:00remaining
Why are Django forms important for web applications?
Which of the following best explains why Django forms are essential in web development?
AThey automatically create database tables without models
BThey replace the need for any JavaScript on the client side
CThey provide automatic HTML generation, data validation, and protection against common security issues like CSRF
DThey allow direct manipulation of the database without validation
Attempts:
2 left
💡 Hint
Think about what problems forms solve beyond just showing input fields.