如何保存用户名(电话)和密码unidecode在Django?

2024-04-19 09:11:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Django中使用一个自定义用户模型。当使用波斯语或阿拉伯语数字注册用户时,我无法登录,因为用户名和密码是用波斯语保存的

在登录窗体中我可以将条目更改为unidecode,但在注册窗体中我不能。 如果我用英语数字注册,我可以用所有数字(英语、波斯语等)登录,但如果我用波斯语或阿拉伯语注册,我不能登录。我如何解决这个问题

我对数字使用Unidecode,对字符字段使用文本Unidecode 在代码中,我试图取消电话(用户名)和注册形式的密码,但我无法让它工作

保存在窗体中:

 def save(self, commit=True):
        # Save the provided password in hashed format
        user = super(RegisterForm, self).save(commit=False)
        password1 = self.cleaned_data.get("password1")
        password1 = unidecode(password1)
        user.set_password(password1)
        user.is_active = True # send confirmation email via signals
        # obj = EmailActivation.objects.create(user=user)
        # obj.send_activation_email()
        if commit:
            user.save()
        return user

在模型中创建用户:

class UserManager(BaseUserManager):
   def create_user(self, phone, name, family, password=None, is_active=True, is_staff=False, is_admin=False):
      if not phone:
         raise ValueError("Users must have an phone")
      if not password:
         raise ValueError("Users must have a password")

      if not name:
         raise ValueError("Users must have a name") 

      if not family:
         raise ValueError("Users must have a family")    
      this_phone       = unide(phone)
      this_password    = unidecode(password)
      user_obj    = self.model(
         phone    = this_phone,
         name     = name,
         family   = family
        )
      user_obj.set_password(this_password) # change user password
      user_obj.is_active = is_active
      user_obj.staff = is_staff
      user_obj.admin = is_admin
      user_obj.save(using=self._db)
      return user_obj

Tags: nameselfobjifissavenotphone