0
0
Djangoframework~10 mins

Form error handling in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the form has errors.

Django
if form.[1]:
    print("Form has errors")
Drag options to blanks, or click blank then click option'
Ais_valid
Bis_error
Cerrors
Dhas_errors
Attempts:
3 left
💡 Hint
Common Mistakes
Using form.is_valid() to check errors (it returns True if no errors).
Trying to call a non-existent method like form.has_errors().
2fill in blank
medium

Complete the code to display non-field errors in a Django template.

Django
{% if form.[1] %}
  <ul>
  {% for error in form.[1] %}
    <li>{{ error }}</li>
  {% endfor %}
  </ul>
{% endif %}
Drag options to blanks, or click blank then click option'
Anon_field_errors
Ball_errors
Cfield_errors
Derrors
Attempts:
3 left
💡 Hint
Common Mistakes
Using form.errors which iterates over field names, not messages.
Trying to use a non-existent attribute like all_errors.
3fill in blank
hard

Fix the error in the code to add a custom error to a form field.

Django
def clean_username(self):
    username = self.cleaned_data.get('username')
    if username == 'admin':
        self.[1]('username', 'This username is not allowed.')
    return username
Drag options to blanks, or click blank then click option'
Aset_error
Braise_error
Cerror_add
Dadd_error
Attempts:
3 left
💡 Hint
Common Mistakes
Using raise_error which is not a form method.
Trying to set errors by assigning directly to a field.
4fill in blank
hard

Fill both blanks to check if the form is valid and then access the errors.

Django
if form.[1]():
    print(form.[2])
Drag options to blanks, or click blank then click option'
Ais_valid
Bis_error
Cerrors
Dhas_errors
Attempts:
3 left
💡 Hint
Common Mistakes
Using is_error() which does not exist.
Trying to access errors before checking validity.
5fill in blank
hard

Fill all three blanks to add a non-field error and then check if the form has any errors.

Django
form.[1](None, '[2]')
if form.[3]:
    print('There are errors')
Drag options to blanks, or click blank then click option'
Aadd_error
BThis is a non-field error
Cerrors
Dis_valid
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a field name instead of None for non-field errors.
Checking is_valid instead of errors to detect errors.