【问题标题】:django send html email with textarea htmldjango 使用 textarea html 发送 html 电子邮件
【发布时间】:2013-10-15 07:52:27
【问题描述】:

我有这个代码:

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()

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

【问题讨论】:

    标签: django email templates django-templates django-email


    【解决方案1】:

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

    from django.utils.safestring import mark_safe
    

    【讨论】:

    • 我使用了safe模板标签,我认为是一样的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    • 2013-06-27
    • 1970-01-01
    • 2015-01-23
    • 2011-03-30
    • 2010-11-22
    • 2014-12-10
    相关资源
    最近更新 更多