Reputation: 2623
I've recently implemented the Django AuthenticationForm into my project and recognized that the error returns some weird str __all__
Returned error message in the frontend when using a wrong password:
My template
<div class="error-wrapper">
<div class="errors">{{ form.errors }}</div>
</div>
So I took a look into the Django source code to check out the form which doesn't include __all__
in the error message str:
class AuthenticationForm(forms.Form):
"""
Base class for authenticating users. Extend this to get a form that accepts
username/password logins.
"""
username = UsernameField(widget=forms.TextInput(attrs={'autofocus': True}))
password = forms.CharField(
label=_("Password"),
strip=False,
widget=forms.PasswordInput(attrs={'autocomplete': 'current-password'}),
)
error_messages = {
'invalid_login': _(
"Please enter a correct %(username)s and password. Note that both "
"fields may be case-sensitive."
),
'inactive': _("This account is inactive."),
}
What do I miss?
Upvotes: 1
Views: 271
Reputation: 477338
The .errors
property [Django-doc] returns a dictionary of errors where the keys represent the field where the error occurred, and the value a list of errors. For errors that are not specific to one field, but to two or more fields, it will use __all__
. These are thus non-field errors.
When you render a Django from in a template, you can use {{ form.non_field_errors }}
to render these, and {{ form.some_field.errors }}
to render the errors dedicates to a certain field. You can also iterate over form.non_field_errors
and form.some_field.errors
to obtain the errors one-by-one, and render these in a different way.
You can thus render non-field errors with:
<div class="error-wrapper">
<div class="errors">{{ form.non_field_errors }}</div>
</div>
Upvotes: 2