[英]Django render_to_string adding '\n' to XML template
[英]Django autoescape is not working in render_to_string(template)?
當用戶成功更新密碼后,我正在為他們起草電子郵件模板。
我在模板中使用了{{autoescape off}},該模板是使用render_to_string()渲染的。
但是,電子郵件內容直接顯示HTML尖括號,如下所示:
嗨,<span style ='color:blue'>用戶! </跨度>
您的密碼更新成功!
views.py
{{ autoescape off}}
Hi <span style='color:blue'>user! </span>
Your password is updated successfully!
{{ endautoescape }}
template.html
{{ autoescape off}} Hi <span style='color:blue'>user! </span> Your password is updated successfully! {{ endautoescape }}
我的代碼有什么問題嗎?
否則,在使用render_to_string()時自動轉義是否始終打開?
這與呈現變量的自動轉義無關(它也是模板標記,因此您將其與{% autoescape off %}
,而不是{{ autoescape off }}
)。 它在當前模板中什么也不做。
您的問題是,您試圖將HTML放入電子郵件的純文本主體中。
send_mail
需要純文本消息正文。 如果要使用HTML正文,則需要提供html_message
參數:
send_mail(
email_title,
'', # Empty plain text body - not recommended and you should supply a plain text alternative
'myemail@email.com',
[recipient,],
html_message=email_content, # This will render as HTML
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.