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

时间:2019-06-20 07:09:42

标签: python django authentication character-encoding python-unicode

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

在“登录”表单中,我可以将条目更改为unidecode,但在“注册”表单中,我不能。 如果我用英文数字注册,那么我可以用所有数字(英语,波斯语等)登录,但是如果我用波斯语或阿拉伯语注册,则无法登录。我该如何解决?

我将Unidecode用于数字,将text-unidecode用于char字段 在代码中,我尝试取消注册表格中的电话(用户名)和密码,但无法正常工作

保存为表单:

 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

0 个答案:

没有答案