django 中的电子邮件模板
众所周知(或应该),您可以使用 Django 的模板系统来渲染电子邮件正文:
def email(email, subject, template, context):
from django.core.mail import send_mail
from django.template import loader, Context
send_mail(subject, loader.get_template(template).render(Context(context)), '[email protected]', [email,])
我认为这有一个缺陷:要编辑电子邮件的主题和内容,您必须同时编辑视图和模板。虽然我可以证明授予管理员用户访问模板的权限,但我不会授予他们访问原始 python 的权限!
如果您可以在电子邮件中指定块并在发送电子邮件时将它们单独拉出,那真是太酷了:
{% block subject %}This is my subject{% endblock %}
{% block plaintext %}My body{% endblock%}
{% block html %}My HTML body{% endblock%}
但是您会怎么做呢?您将如何一次只渲染一个块?
As we all know (or should), you can use Django's template system to render email bodies:
def email(email, subject, template, context):
from django.core.mail import send_mail
from django.template import loader, Context
send_mail(subject, loader.get_template(template).render(Context(context)), '[email protected]', [email,])
This has one flaw in my mind: to edit the subject and content of an email, you have to edit both the view and the template. While I can justify giving admin users access to the templates, I'm not giving them access to the raw python!
What would be really cool is if you could specify blocks in the email and pull them out separately when you send the email:
{% block subject %}This is my subject{% endblock %}
{% block plaintext %}My body{% endblock%}
{% block html %}My HTML body{% endblock%}
But how would you do that? How would you go about rendering just one block at a time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我的第三次工作迭代。假设您有一个像这样的电子邮件模板:
我已经重构为默认情况下迭代通过列表发送的电子邮件,并且有一些实用方法用于发送到单个电子邮件和
django.contrib.auth
用户(单个和多个)。我所涵盖的内容可能超出了我实际需要的范围,但是就这样吧。
我也可能对 Python 的热爱有些过头了。
一如既往,如果你能改进这一点,请这样做。
This is my third working iteration. It assuming you have an email template like so:
I've refactored to iterate the email sending over a list by default and there are utility methods for sending to a single email and
django.contrib.auth
User
s (single and multiple). I'm covering perhaps more than I'll sensibly need but there you go.I also might have gone over the top with Python-love.
As ever, if you can improve on that, please do.
只需使用两个模板:一个用于正文,一个用于主题。
Just use two templates: one for the body and one for the subject.
我无法使用
{% body %}
标签使模板继承工作,因此我切换到如下模板:现在我们必须渲染模板三次,但继承工作正常。
我还发现在渲染非 HTML 文本时有必要关闭自动转义以避免电子邮件中的转义文本
I couldn't get template inheritance to work using the
{% body %}
tags, so I switched to a template like this:Now we have to render the template three times, but the inheritance works properly.
I also found it necessary to turn off autoescape when rendering the non-HTML text to avoid escaped text in the email