【问题标题】:Change default django error messages for AuthenticationForm更改 AuthenticationForm 的默认 django 错误消息
【发布时间】:2018-06-19 20:05:19
【问题描述】:

我目前使用这种替代方案AuthenticationForm

class AuthenticationForm(AuthenticationForm):
    username = forms.CharField(widget=forms.HiddenInput(), initial="members")

    def confirm_login_allowed(self, user):
        pass

    def __init__(self, *args, **kwargs):
        super(AuthenticationForm, self).__init__(*args, **kwargs)
        self.fields['password'].error_messages = {'incomplete': 'Please enter a valid password.'}

        for field in self.fields.values():
            field.error_messages = {'required':'T {fieldname} is required'.format(
            fieldname=field.label)}

其中一些代码无用,但我正在尝试更改用户输入错误密码时的错误消息。目前,django 提供错误消息:Please enter a correct username and password. Note that both fields may be case-sensitive.,但我希望它是 Please enter the correct password

我想这是一个边缘情况。

感谢您的帮助。

【问题讨论】:

标签: django


【解决方案1】:

你可以试试吗?

class AuthenticationForm(AuthenticationForm):
    username = forms.CharField(widget=forms.HiddenInput(), initial="members")
    password = forms.CharField(error_messages={'incomplete': 'Please enter a valid password.'})

【讨论】:

【解决方案2】:

转到 AuthenticationForm 类,然后编辑 error_messages...

---这是图片--- AuthenticationForm ---这是图片---

【讨论】:

    猜你喜欢
    • 2012-01-24
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 2021-04-15
    • 1970-01-01
    • 2021-12-30
    相关资源
    最近更新 更多