1

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

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

4

1 回答 1

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
于 2013-02-28T12:20:04.333 回答