0

我有这个代码:

def send(modeladmin, request, queryset):
    emails = []
    for email in queryset.get().listing.emails.all():
        if email.is_valid():
            emails.append(email.email)

    from django.core.mail import EmailMultiAlternatives
    from django.template.loader import render_to_string

    template_data = {
        'body': '<p style="color:red">asdasdas</p>'
    }

    html_body = render_to_string("template.html", template_data)
    from_email = "%s <%s>" % (queryset.get().from_identity.full_name, queryset.get().from_identity.email)
    msg = EmailMultiAlternatives(subject=queryset.get().subject, from_email=from_email, to=emails, headers={})
    msg.attach_alternative(html_body, "text/html")

    msg.send()

现在,我收到了带有模板的电子邮件,但是这个“正文”数据被转义了,我怎样才能在不转义的情况下进行渲染?

4

1 回答 1

1

您可以将 html_body 标记为安全mark_safe()

from django.utils.safestring import mark_safe
于 2013-10-08T12:02:01.447 回答