如何在Django用户名中允许空格? [英] How can I allow spaces in a Django username?

查看:470
本文介绍了如何在Django用户名中允许空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Django应用中允许用户名中的空格。这是从我的形式:

I'm trying to allow spaces in usernames in my Django app. This is from my form:

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

我希望以一个字母开头的用户名,然后我允许使用字母和空格和连字符。 我不知道为什么,其他地方的空格有限制吗?

I want username to start with a letter. After that I allow letters and spaces and hyphens. Somehow the form fails validation with "John Smith" and I have no idea why. Is there a restriction on spaces somewhere else?

推荐答案

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

the django auth user is based on an abstract user class (django.contrib.auth.models). Your bound form may be valid, but if you try to save the user object validation fails.

查看来自django.contrib.auth.models的用户名:

Have a look at the source from django.contrib.auth.models regarding the username:

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')
        ])

希望对此有所了解。

这篇关于如何在Django用户名中允许空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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