سؤال

I have this code:

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

and now, i got email with template, but this "body" data is escaped, how can I make render without escaping?

هل كانت مفيدة؟

المحلول

You can mark the html_body as safe with mark_safe()

from django.utils.safestring import mark_safe
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top