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.
2 Answers
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 }}
3 Comments
Bono
Note that according to the Django docs your backend should function as following; "check the credentials it gets, and it should return a User object that matches those credentials, if the credentials are valid. If they’re not valid, it should return None". Not sure if you were suggesting to raise a
ValidationError in the backend, but I'm just putting it out there.Sebastián Vansteenkiste
@Bono, though you're right (and I like your music), facing the alternative of having to implement your own form, simply raising a ValidationError within the auth method is awfully simple and just works.
Sebastián Vansteenkiste
(of course, the idea is not to throw this for when a user enters "wrong" credentials as-in ones that don't match an existing user, but "wrong" in a sense that is considered invalid because of some other validation logic pertinent for this particular auth backend)