2017-07-31 92 views
0

我有一個create_user_profile信號,我想使用相同的信號向用戶發送歡迎電子郵件。Django在用戶使用信號創建後發送歡迎郵件

這是我寫的,我signals.py至今:

@receiver(post_save, sender=User) 
def update_user_profile(sender, instance, created, **kwargs): 
    if created: 
     UserProfile.objects.create(user=instance) 
    instance.profile.save() 

    subject = 'Welcome to MyApp!' 
    from_email = '[email protected]' 
    to = instance.email 
    plaintext = get_template('email/welcome.txt') 
    html = get_template('email/welcome.html') 

    d = Context({'username': instance.username}) 

    text_content = plaintext.render(d) 
    html_content = html.render(d) 

    try: 
     msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) 
     msg.attach_alternative(html_content, "text/html") 
     msg.send() 
    except BadHeaderError: 
     return HttpResponse('Invalid header found.') 

這是得到這個錯誤:

TypeError at /signup/ 
context must be a dict rather than Context. 

指着我的意見forms.save。 py文件。 你能幫我理解這裏有什麼問題嗎?

回答

1

只是傳遞一個字典來渲染,而不是一個Context對象

d = {'username': instance.username} 
text_content = plaintext.render(d) 
相關問題