繁体   English   中英

如何从django模板中删除所有html标签?

[英]How to remove all html tags from django template?

我想用django模板进行电子邮件发送。 但我应该有两个版本的电子邮件 - HTML和纯文本。 第一个版本生成如下:

template_values = {
     'company_id': company.id
     }
template_file = os.path.join(os.path.dirname(__file__), 'templates/email.html')
html = template.render(template_file, template_values)

现在我应该从同一个文件生成纯文本 - templates/email.html ,但它包含html标签(如<div style="font-size:13px; margin: 14px; position:relative"> ),我应该删除(链接应保留,但应替换为纯文本,例如, <a href="http://example.com">Example</a>应替换为Example (http://example.com) )。

我已经读过,我不应该使用正则表达式。 什么样的内置GAE库可以帮助我? 或者有没有办法以某种方式使用django的striptags

获得HTML后,您需要执行以下操作:

from django.utils.html import strip_tags

template_values = {
     'company_id': company.id
}
template_file = os.path.join(os.path.dirname(__file__), 'templates/email.html')
html = template.render(template_file, template_values)

plain_text = strip_tags(html)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM