让django 支持中文注册登录,支持中文用户名
cat django的models文件发现调的如下两个类
class AbstractUser(AbstractBaseUser, PermissionsMixin):
"""
An abstract base class implementing a fully featured User model with
admin-compliant permissions.
Username and password are required. Other fields are optional.
"""
username_validator = UnicodeUsernameValidator() if six.PY3 else ASCIIUsernameValidator()
username = models.CharField(
_('username'),
max_length=150,
unique=True,
help_text=_('Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.'),
validators=[username_validator],
error_messages={
'unique': _("A user with that username already exists."),
},
)
修改他的正则匹配规则
[root@localhost auth]# cat validators.py
import re
from django.core import validators
from django.utils import six
from django.utils.deconstruct import deconstructible
from django.utils.translation import ugettext_lazy as _
@deconstructible
class ASCIIUsernameValidator(validators.RegexValidator):
#regex = r'^[\w.@+-]+$'
regex = r'^[\S.@+-]+$'
message = _(
'Enter a valid username. This value may contain only English letters, '
'numbers, and @/./+/-/_ characters.'
)
flags = re.ASCII if six.PY3 else 0
@deconstructible
class UnicodeUsernameValidator(validators.RegexValidator):
#regex = r'^[\w.@+-]+$'
regex = r'^[\S.@+-]+$'
message = _(
'Enter a valid username. This value may contain only letters, '
'numbers, and @/./+/-/_ characters.'
)
flags = re.UNICODE if six.PY2 else 0
[root@localhost auth]# pwd
/usr/local/python27/lib/python2.7/site-packages/django/contrib/auth
[root@localhost auth]#