2013-02-04 95 views
1

我正在使用django-1.4.3,超級用戶可以創建用戶併爲其分配密碼。在這裏,我使用django-email-as-username使用戶能夠以他們的電子郵件作爲用戶名登錄。當超級用戶添加新用戶時,新添加的用戶應該通過電子郵件通知他的用戶名和密碼。Django admin - 在用戶創建時的電子郵件用戶名和密碼

用戶創建後,我可以使用post_save signal發送電子郵件。但是我無法獲得密碼,因爲它會被加密和存儲。我想給用戶發送原始密碼。我怎樣才能做到這一點?

回答

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 
+0

非常感謝你幫我,但請你解釋你梅索德一點點,特別inspect.stack – Armance

相關問題