我有一个create_user_profile信号,我想使用相同的信号向用户发送欢迎电子邮件。
这是我到目前为止在 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 = 'no-reply@myapp.com'
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.
指向views.py文件中的forms.save。 你能帮我理解这里的错误吗?
答案 0 :(得分:1)
在django 1.11上,模板上下文必须是dict: https://docs.djangoproject.com/en/1.11/topics/templates/#django.template.backends.base.Template.render
尝试删除Context对象creationg。
d = {'username': instance.username}
答案 1 :(得分:1)
只需将dict传递给渲染而不是Context对象
d = {'username': instance.username}
text_content = plaintext.render(d)