2

我试图在我的 Django 应用程序中允许用户名中有空格。这是来自我的表格:

class SignUpForm(django.forms.ModelForm):
...
    username = django.forms.RegexField(
       regex=r'^[a-zA-Z]{1}[a-zA-Z -]{1,29}$'

我希望用户名以字母开头。之后,我允许使用字母、空格和连字符。不知何故,表单无法通过“John Smith”验证,我不知道为什么。其他地方有空间限制吗?

4

1 回答 1

3

django auth 用户基于一个抽象用户类(django.contrib.auth.models)。您的绑定表单可能有效,但如果您尝试保存用户对象验证失败。

查看 django.contrib.auth.models 关于用户名的来源:

class AbstractUser(AbstractBaseUser, PermissionsMixin):
    """
An abstract base class implementing a fully featured User model with
admin-compliant permissions.

Username, password and email are required. Other fields are optional.
"""
    username = models.CharField(_('username'), max_length=30, unique=True,
        help_text=_('Required. 30 characters or fewer. Letters, digits and '
                    '@/./+/-/_ only.'),
        validators=[
            validators.RegexValidator(r'^[\w.@+-]+$', _('Enter a valid username.'), 'invalid')
        ])

希望这能对此有所启发。

于 2013-11-11T17:23:22.773 回答