Django - 自定义身份验证后端中的错误消息

发布于 2024-10-29 06:33:16 字数 78 浏览 7 评论 0原文

我通过扩展默认的 ModelBackend 编写了一个自定义身份验证后端。是否可以向登录屏幕发送自定义错误消息?截至目前,它显示的是默认消息。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡写薰衣草的香 2024-11-05 06:33:16

您可以引发 django ValidationError

from django.core.exceptions import ValidationError

raise ValidationError("Some custom message")

然后可以使用以下命令显示它:

{{ form.non_field_errors|striptags }}

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 }}
梦忆晨望 2024-11-05 06:33:16

错误消息是由 django.contrib.auth.forms.AuthenticationForm 引发的 ValidationError 异常。您需要扩展身份验证表单或实现您自己的身份验证表单来更改其错误消息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文