Complete the code to check if the form has errors.
if form.[1]: print("Form has errors")
The form.errors attribute returns a dictionary of errors if the form has any. Checking it directly tells if there are errors.
Complete the code to display non-field errors in a Django template.
{% if form.[1] %}
<ul>
{% for error in form.[1] %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}In Django templates, form.non_field_errors is an ErrorList containing non-field errors for the form. It can be iterated to display messages.
Fix the error in the code to add a custom error to a form field.
def clean_username(self): username = self.cleaned_data.get('username') if username == 'admin': self.[1]('username', 'This username is not allowed.') return username
The add_error method adds an error message to a specific field in the form.
Fill both blanks to check if the form is valid and then access the errors.
if form.[1](): print(form.[2])
First, form.is_valid() checks if the form data is valid. form.errors contains the error details if invalid (empty dict if valid).
Fill all three blanks to add a non-field error and then check if the form has any errors.
form.[1](None, '[2]') if form.[3]: print('There are errors')
Use add_error(None, message) to add a non-field error. Then check form.errors to see if any errors exist.