【发布时间】: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 所以你之前的问题已经解决了,那么这是一个完全不相关的问题吗?
-
我删除了第二个。这个案子对我来说更有趣,而且可能更容易解决。