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

LA_*_*LA_ 3 python django google-app-engine django-templates

我想用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)
Run Code Online (Sandbox Code Playgroud)

现在我应该从同一个文件生成纯文本 - 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

pyr*_*iku 7

获得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)
Run Code Online (Sandbox Code Playgroud)