Django正则表达式验证器消息无效 [英] Django regex validator message has no effect

查看:241
本文介绍了Django正则表达式验证器消息无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到它,以便验证器告诉你用户名必须是字母数字。这是我的代码到目前为止。我已经确认它在正确的时间验证。唯一的问题是,无论我尝试什么,RegexValidator仍然会抓住默认错误(输入有效值)。

I am trying to get it so that the validator tells you "username must be alphanumeric". This is my code so far. I have confirmed that it validates at the correct time. The only problem is that no matter what I try, The RegexValidator still chucks the default error ("enter a valid value").

这是我的代码。我还尝试了没有'message ='在前面,它仍然说输入有效的值,而不是用户名必须是字母数字

This is my code. I also tried it without the 'message=' in front, and it still said "enter a valid value", instead of "username must be alphanumeric"

user = CharField(
    max_length=30,required=True,
    validators=[
        RegexValidator('^[a-zA-Z0-9]*$',
            message='Username must be Alphanumeric'
        ),
    ]
)


推荐答案

添加错误代码如何:

user = CharField(
    max_length=30,
    required=True,
    validators=[
        RegexValidator(
            regex='^[a-zA-Z0-9]*$',
            message='Username must be Alphanumeric',
            code='invalid_username'
        ),
    ]
)

这篇关于Django正则表达式验证器消息无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆