【发布时间】:2018-05-23 10:21:54
【问题描述】:
我正在为成功更新密码的用户起草电子邮件模板。
我在模板中使用了 {{ autoescape off }},通过 render_to_string() 进行渲染。
但是,电子邮件内容直接显示 HTML 尖括号,如下所示:
您好用户!
您的密码更新成功!
我正在使用 Django2.0,我的代码如下所示:
views.py
from django.core.mail import send_mail
from django.template.loader import render_to_string
def sendmail(request, title)
email_title = title
email_content = render_to_string('template.html',{'username':request.user.username})
recipient = request.user.email
send_mail(
email_title,
email_content,
'myemail@email.com',
[recipient,],
)
template.html
{{ autoescape off}}
Hi <span style='color:blue'>user! </span>
Your password is updated successfully!
{{ endautoescape }}
我的代码有什么问题吗?
否则,在使用 render_to_string() 时是否始终开启自动转义?
【问题讨论】:
-
这与自动转义无关。这适用于从变量插入模板的 HTML,您没有这样做。
标签: python html django render-to-string