实例的某些字段未显示在我的电子邮件模板中。如下所示,不显示字段user_phone。
我有查看获取或创建用户的信息:
views.py
# Get informations for creating the User
user, created = User.objects.get_or_create(
username=username,
email=email,
first_name=first_name,
last_name=last_name,
)
if created:
user.groups.add(Group.objects.get(name='Clients'))
user.profile.phone = telephone
user.save()
else:
user.profile.phone = telephone
user.save()
我发送电子邮件给有这个信息的新用户
models.py
@receiver(post_save, sender=User)
def password_mail(sender, instance, **kwargs):
if kwargs['created']:
user_email = instance.email
first_name = instance.first_name
user_phone = instance.profile.phone
subject, from_email, to = 'New account', 'mail@example.com', user_email
text_content = render_to_string('accounts/mail_welcome.txt', {'first_name': first_name, 'user_phone': user_phone})
html_content = render_to_string('accounts/mail_welcome.html', {'first_name': first_name, 'user_phone': user_phone})
# create the email, and attach the HTML version as well.
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()
最后,这是电子邮件模板:
mail_welcome.txt
{% autoescape off %}
Hello {{ first_name }},
Here is you phone number : {{ user_phone }}
{% endautoescape %}
除user_phone外,所有标签都显示良好。 这是因为我在视图中保存此字段的方式吗?
帮助赞赏:)