在创建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='}
哈希密码的值不同,但我无法使用&#34; abcd&#34;登录。是什么原因?
行。在这种情况下,它开始&#34;工作。但在我的管理形式的逻辑中,它仍然没有:
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
通过电子邮件密码提供的密码不允许我登录,也无法在控制台中进行身份验证。
答案 0 :(得分:0)
WAS与发送电子邮件无关,但我使用表单方法错误的原因。
这是基本形式的工作解决方案(没有发送电子邮件,也没有以某种随机的方式生成新密码),以便让其他人明白这一点,而这些人在调用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