【问题标题】:How to change password of user in AbstractBaseUser如何在 AbstractBaseUser 中更改用户密码
【发布时间】:2014-04-02 14:39:21
【问题描述】:

在创建 MyUser 时,我关注的是:

https://docs.djangoproject.com/en/1.6/topics/auth/customizing/#a-full-example

我想修改用户密码。

我试试:

In [55]: i=FairsUser.objects.get(email="admin@andilabs.com")

In [56]: i.__dict__
Out[56]:
{'_state': <django.db.models.base.ModelState at 0x110a53490>,
 'email': u'admin@andilabs.com',
 'id': 8,
 'is_active': True,
 'is_admin': False,
 'is_superuser': False,
 'last_login': datetime.datetime(2014, 4, 2, 16, 0, 59, 109673),
 'mail_sent': False,
 'password': u'pbkdf2_sha256$12000$XgszxXkXbroY$PEEf3vqszclGcf7iQXeZRWDTYcCsvlGh0jH15f6rKR8='}

In [57]: i.set_password("abcd")

In [58]: i.save()

然后我检查:

In [59]: i_updated=FairsUser.objects.get(email="admin@andilabs.com")

In [60]: i_updated.__dict__
Out[60]:
{'_state': <django.db.models.base.ModelState at 0x110a53590>,
 'email': u'admin@andilabs.com',
 'id': 8,
 'is_active': True,
 'is_admin': False,
 'is_superuser': False,
 'last_login': datetime.datetime(2014, 4, 2, 16, 0, 59, 109673),
 'mail_sent': False,
 'password': u'pbkdf2_sha256$12000$8VCDlzTuVfHF$ldwqbXo/axzMFLasqOKkddz8o1yW9d5r7gUxD3qH4sU='}

散列密码的值不同,但我无法使用“abcd”登录。是什么原因?

好的。在这种情况下,它“开始”工作。但在我的管理员形式的逻辑中,它仍然没有:

def clean(self, commit=True):
    super(UserChangeForm, self).clean()
    cleaned_data = self.cleaned_data
    old_status = FairsUser.objects.get(email=cleaned_data['email'])
    if (old_status.mail_sent is False or old_status.mail_sent is None) and cleaned_data['mail_sent'] is True:
        usr = FairsUser.objects.get(email=cleaned_data['email'])
        new_pass = ''.join(
            random.choice(
                string.ascii_uppercase + string.digits
                ) for _ in range(DESIRED_PASSWORD_LENGTH))
        usr.set_password(new_pass)
        usr.save()
        mail_content = 'Hi! Bbelow are your login credentials.\n e-mail: {0} \n password: {1} \n Have nice time on fairs! \n'.format(usr.email, new_pass)
        msg = EmailMessage('Your login data', mail_content, 'from@andilabs.com', [usr.email, ])
        msg.send()
    return cleaned_data

邮件发送的密码不允许我登录,我也无法在控制台中进行身份验证。

【问题讨论】:

  • 您没有显示失败的authenticate()。请粘贴完整的 Python 会话,您可以在其中显示 set_password('abcd') 后跟 authenticate(user, 'abcd') 实际上失败了。
  • 这是您之前问题的副本:AbstractBaseUser change password via set_password not working。如果您需要添加更多详细信息,请编辑您现有的问题,而不是针对同一问题发布新问题。
  • @Ianzz 请注意:在 MODEL 和 FORM 中尝试思想的区别。
  • @andi 所以你之前的问题已经解决了,那么这是一个完全不相关的问题吗?
  • 我删除了第二个。这个案子对我来说更有趣,而且可能更容易解决。

标签: python django


【解决方案1】:

原因与发送电子邮件无关,而是我错误地使用了表单方法。

这是基本形式的工作解决方案(不发送电子邮件,并且不以某种随机方式生成新密码)以使其他人清楚,他们在调用 save() 时遇到相同的 set_password 问题AbstractBaseUser 的形式

def save(self, commit=True):
    user = super(UserChangeForm, self).save(commit=False)
    cleaned_data = self.cleaned_data
    old_status = FairsUser.objects.get(email=cleaned_data['email'])
    if (old_status.mail_sent is False or old_status.mail_sent is None) and cleaned_data['mail_sent'] is True:
        new_pass =  "SOME_NEW_VALUE_OF_PASSWORD"
        user.set_password(new_pass)
        # here sending email can be initiated
    if commit:
        user.save()
    return user

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    相关资源
    最近更新 更多