0
0
Djangoframework~20 mins

Form error handling in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Form Error Handling Master
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 invalid?
Consider a Django form instance that has been submitted with invalid data. What will be the state of form.errors and what will the template typically display?
Django
form = MyForm(data=request.POST)
if not form.is_valid():
    errors = form.errors
Aform.errors is empty; the template shows no errors because the form is invalid.
Bform.errors contains a dictionary of field names to error messages; the template shows these errors next to the fields.
Cform.errors contains a list of error strings without field names; the template shows a generic error message only.
Dform.errors raises an exception if the form is invalid; the template crashes.
Attempts:
2 left
💡 Hint
Think about how Django helps users know what went wrong with their input.
📝 Syntax
intermediate
2:00remaining
Which code correctly adds a non-field error to a Django form?
You want to add a custom error message that is not tied to any specific field. Which code snippet correctly does this inside a Django form's clean() method?
Django
def clean(self):
    cleaned_data = super().clean()
    if some_condition:
        # add non-field error here
    return cleaned_data
Aself.non_field_errors.append('Custom non-field error message')
Bself.errors['__all__'] = 'Custom non-field error message'
Cself.add_error(None, 'Custom non-field error message')
Dself.add_error('non_field', 'Custom non-field error message')
Attempts:
2 left
💡 Hint
Check Django docs for adding errors not tied to fields.
🔧 Debug
advanced
2:00remaining
Why does this Django form not show errors in the template?
Given this view code snippet, why might the form errors not appear in the template?

form = MyForm(request.POST or None) if form.is_valid(): # process data else: form = MyForm()
AThe form is reinitialized with empty data in the else block, so errors are lost.
BThe form is valid, so no errors exist to show.
CThe template does not have access to the form variable.
DDjango automatically clears errors after is_valid() check.
Attempts:
2 left
💡 Hint
Look at how the form variable is assigned in the else block.
state_output
advanced
2:00remaining
What is the output of this Django form error check?
Consider this code snippet inside a Django form's clean() method:

if self.cleaned_data.get('age', 0) < 18: self.add_error('age', 'Must be at least 18') return self.cleaned_data

What will form.errors contain if the submitted age is 16?
A{'age': ['Must be at least 18']}
B{} (empty dictionary)
C{'__all__': ['Must be at least 18']}
DRaises a KeyError because 'age' is missing
Attempts:
2 left
💡 Hint
Check how add_error associates messages with fields.
🧠 Conceptual
expert
2:00remaining
Which statement about Django form error handling is true?
Select the one true statement about how Django handles form errors during validation.
ADjango stores all errors as a flat list in <code>form.errors</code>, without distinguishing fields.
BNon-field errors must be added by raising exceptions inside the form's <code>clean()</code> method.
CCalling <code>form.is_valid()</code> clears all previous errors automatically.
DField-specific errors are stored in <code>form.errors</code> under their field names, while non-field errors are stored under the key <code>__all__</code>.
Attempts:
2 left
💡 Hint
Think about how Django separates errors for fields and general form errors.