Django - Error Message in Custom Auth Backend

2019-07-07 19:36发布

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条回答
仙女界的扛把子
2楼-- · 2019-07-07 20:00

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 }}
查看更多
Melony?
3楼-- · 2019-07-07 20:02

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.

查看更多
登录 后发表回答