Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从django模板中删除所有html标记?_Python_Django_Google App Engine_Django Templates - Fatal编程技术网

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

Python 如何从django模板中删除所有html标记?,python,django,google-app-engine,django-templates,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

我想使用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标记(如
),我应该删除这些标记(链接应该保留,但应该替换为纯文本,例如,
应该替换为类似
示例的内容)(http://example.com)


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

一旦获得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)

实现这一点的常用方法是对其他电子邮件格式“文本”使用另一个模板。@jpic,谢谢。如果我使用单独的纯文本django模板,看起来行尾字符被删除了。我怎样才能避免呢?