-->

Django - Error Message in Custom Auth Backend

2019-07-07 19:42发布

问题:

I have written a custom auth backend by extending the defalut ModelBackend. Is it possible to send a custom error message to login screen? As of now it is displaying the default message.

回答1:

The error messages are coming as ValidationError exceptions raised by django.contrib.auth.forms.AuthenticationForm. You would need to extend the Authentication Form or implement your own authentication form to change it's error messages.



回答2:

You can raise a django ValidationError

from django.core.exceptions import ValidationError

raise ValidationError("Some custom message")

You can then display it with:

{{ form.non_field_errors|striptags }}