django 中的电子邮件模板

发布于 2024-08-17 14:14:56 字数 767 浏览 9 评论 0原文

众所周知(或应该),您可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仅一夜美梦 2024-08-24 14:14:56

这是我的第三次工作迭代。假设您有一个像这样的电子邮件模板:

{% block subject %}{% endblock %}
{% block plain %}{% endblock %}
{% block html %}{% endblock %}

我已经重构为默认情况下迭代通过列表发送的电子邮件,并且有一些实用方法用于发送到单个电子邮件和django.contrib.auth 用户(单个和多个)。我所涵盖的内容可能超出了我实际需要的范围,但是就这样吧。

我也可能对 Python 的热爱有些过头了。

def email_list(to_list, template_path, context_dict):
    from django.core.mail import send_mail
    from django.template import loader, Context

    nodes = dict((n.name, n) for n in loader.get_template(template_path).nodelist if n.__class__.__name__ == 'BlockNode')
    con = Context(context_dict)
    r = lambda n: nodes[n].render(con)

    for address in to_list:
        send_mail(r('subject'), r('plain'), '[email protected]', [address,])

def email(to, template_path, context_dict):
    return email_list([to,], template_path, context_dict)

def email_user(user, template_path, context_dict):
    return email_list([user.email,], template_path, context_dict)

def email_users(user_list, template_path, context_dict):
    return email_list([user.email for user in user_list], template_path, context_dict)

一如既往,如果你能改进这一点,请这样做。

This is my third working iteration. It assuming you have an email template like so:

{% block subject %}{% endblock %}
{% block plain %}{% endblock %}
{% block html %}{% endblock %}

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 Users (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.

def email_list(to_list, template_path, context_dict):
    from django.core.mail import send_mail
    from django.template import loader, Context

    nodes = dict((n.name, n) for n in loader.get_template(template_path).nodelist if n.__class__.__name__ == 'BlockNode')
    con = Context(context_dict)
    r = lambda n: nodes[n].render(con)

    for address in to_list:
        send_mail(r('subject'), r('plain'), '[email protected]', [address,])

def email(to, template_path, context_dict):
    return email_list([to,], template_path, context_dict)

def email_user(user, template_path, context_dict):
    return email_list([user.email,], template_path, context_dict)

def email_users(user_list, template_path, context_dict):
    return email_list([user.email for user in user_list], template_path, context_dict)

As ever, if you can improve on that, please do.

始终不够爱げ你 2024-08-24 14:14:56

只需使用两个模板:一个用于正文,一个用于主题。

Just use two templates: one for the body and one for the subject.

何时共饮酒 2024-08-24 14:14:56

我无法使用 {% body %} 标签使模板继承工作,因此我切换到如下模板:

{% extends "base.txt" %}

{% if subject %}Subject{% endif %}
{% if body %}Email body{% endif %}
{% if html %}<p>HTML body</p>{% endif %}

现在我们必须渲染模板三次,但继承工作正常。

c = Context(context, autoescape = False)
subject = render_to_string(template_name, {'subject': True}, c).strip()
body = render_to_string(template_name, {'body': True}, c).strip()
c = Context(context, autoescape = True)
html = render_to_string(template_name, {'html': True}, c).strip()

我还发现在渲染非 HTML 文本时有必要关闭自动转义以避免电子邮件中的转义文本

I couldn't get template inheritance to work using the {% body %} tags, so I switched to a template like this:

{% extends "base.txt" %}

{% if subject %}Subject{% endif %}
{% if body %}Email body{% endif %}
{% if html %}<p>HTML body</p>{% endif %}

Now we have to render the template three times, but the inheritance works properly.

c = Context(context, autoescape = False)
subject = render_to_string(template_name, {'subject': True}, c).strip()
body = render_to_string(template_name, {'body': True}, c).strip()
c = Context(context, autoescape = True)
html = render_to_string(template_name, {'html': True}, c).strip()

I also found it necessary to turn off autoescape when rendering the non-HTML text to avoid escaped text in the email

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文