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 }}