Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all html tags from django template?

I would like to use django template for e-mail sending. But I should have two version of e-mails - html and plain text. The first version is generated like below:

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)

Now I should generate plain text from the same file - templates/email.html, but it contains html tags (like <div style="font-size:13px; margin: 14px; position:relative">), which I should remove (links should stay, but should be replaced with plain text, for ex., <a href="http://example.com">Example</a> should be replaced with something like Example (http://example.com)).

I've read that I shouldn't use regex for this. What kind of built-in GAE library can help me? Or is there any way to use somehow django's striptags?

like image 279
LA_ Avatar asked Feb 19 '23 18:02

LA_


1 Answers

Once you get the HTML, you need to do something like:

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)
like image 91
pyriku Avatar answered Feb 27 '23 10:02

pyriku