Python Django检查用户名是否以特殊字符开头,并将其更改为使用更改后的用户名进行身份验证

Python Django检查用户名是否以特殊字符开头,并将其更改为使用更改后的用户名进行身份验证,python,django,django-authentication,Python,Django,Django Authentication,这是我的自定义身份验证后端。我想检查用户名是否以某个字符开头,并将此字符更改为新的字符串或字符 class PhoneAuthBackend(object): def authenticate(self, request, username=None, password=None): try: user = User.objects.get(username=username) if username.startswith(

这是我的自定义身份验证后端。我想检查用户名是否以某个字符开头,并将此字符更改为新的字符串或字符

class PhoneAuthBackend(object):
    def authenticate(self, request, username=None, password=None):
        try:
            user = User.objects.get(username=username)
            if username.startswith('8'):
                username[0].replace('+7')
            if user.check_password(password):
                return user
        except User.DoesNotExist:
            return None

我试过:

class PhoneAuthBackend(object):
    def authenticate(self, request, username=None, password=None):
        try:
            user = User.objects.get(username=username)
            bad_prefix = '8'
            good_prefix = '+7'
            if username.startswith(bad_prefix):
                username = good_prefix + username[len(bad_prefix):]
            if user.check_password(password):
                return user
        except User.DoesNotExist:
            return None
它不起作用

如果我使用此代码,它也不起作用:

            if username.startswith(bad_prefix):
                new_username = good_prefix + username[len(bad_prefix):]
            user = User.objects.get(new_username=username)
解决方案:

class PhoneAuthBackend(object):
    def authenticate(self, request, username=None, password=None):
        try:
            bad_prefix = '8'
            good_prefix = '+7'
            if username.startswith(bad_prefix):
                username = good_prefix + username[len(bad_prefix):]
            user = User.objects.get(username=username)
            if user.check_password(password):
                return user
        except User.DoesNotExist:
            return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None
你不可能真的那样做。切片字符串会更好

>>> username = '8username'
>>> bad_prefix = '8'
>>> good_prefix = '+7'
>>> if username.startswith(bad_prefix): username = good_prefix + username[len(bad_prefix):]
>>> username
'+7username'

你不可能真的那样做。切片字符串会更好

>>> username = '8username'
>>> bad_prefix = '8'
>>> good_prefix = '+7'
>>> if username.startswith(bad_prefix): username = good_prefix + username[len(bad_prefix):]
>>> username
'+7username'


你的实际问题是什么?我不知道如何实现我想要的。这段代码不起作用。你需要提供更多关于你所做的事情的信息-你是如何使用这个类的?我正在制作一个自定义的auth后端来处理上面提到的功能。我已经有一个后端使用电子邮件,而不是用户名。我真的不知道如何检查用户的身份验证输入,更改它并登录。您的实际问题是什么?我不知道如何实现我想要的。这段代码不起作用。你需要提供更多关于你所做的事情的信息-你是如何使用这个类的?我正在制作一个自定义的auth后端来处理上面提到的功能。我已经有一个后端使用电子邮件,而不是用户名。我真的不知道如何检查用户的身份验证输入,更改它并登录。当您运行
user.check\u password(password)
user时,它仍然有旧用户名,并且不是一个动态更改它的好地方。您可能应该在创建用户用户名时更改它。我想让用户使用不同类型的书写电话号码登录(在美国为+7或+1,用8代替+7,用7不带+和根本不带第一位数字)然后,您可能应该将are代码存储在一列中,将原始手机号码存储在另一列中,或者不允许全部更改,并且始终使用完整号码而不执行您正在执行的操作。在调用
User.objects.get(username=username)
Cool之前,您需要更改用户名!然后:)当您运行
user时。检查\u password(password)
user仍然有旧用户名,这不是一个动态更改用户名的好地方。您可能应该在创建用户用户名时更改它。我想让用户使用不同类型的书写电话号码登录(在美国为+7或+1,用8代替+7,用7不带+和根本不带第一位数字)然后,您可能应该将are代码存储在一列中,将原始手机号码存储在另一列中,或者不允许全部更改,并且始终使用完整号码而不执行您正在执行的操作。在调用
User.objects.get(username=username)
Cool之前,您需要更改用户名!然后:)