Django admin - 用户创建时的用户名和密码

时间:2013-02-04 06:27:18

标签: django-admin django-authentication django-1.4

我正在使用django-1.4.3,超级用户可以创建用户并为他们分配密码。在这里,我使用django-email-as-username使用户能够使用他们的电子邮件作为用户名登录。当超级用户添加新用户时,应通过电子邮件通知新添加的用户他的用户名和密码。

我可以在使用post_save signal创建用户后发送电子邮件。但我无法获取密码,因为它将被加密和存储。我想给用户发送电子邮件,原始密码。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

我使用以下代码实现了它:

from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver

@receiver(post_save, sender = User)
def my_callback(sender, **kwargs):
    import inspect
    records =[]
    for frame_record in inspect.stack():
        records.append(frame_record[3])
        if frame_record[3]=='get_response':
            request = frame_record[0].f_locals['request']
            email = request.POST.get('email')
            password1 =  request.POST.get('password1')
            password2 = request.POST.get('password2')
            if email != None and password1 != None and password2 != None and password1 == password2:
                html_content ="Hi,<br> Your username: %s <br> Password: %s"
                from_email   = settings.DEFAULT_FROM_EMAIL
                message      = EmailMessage('Welcome', html_content %(email, password1), from_email, [email])
                message.content_subtype = "html"  # Main content is now text/html
                message.send()
            break