Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter the html markups when render a template with jinja2?

Now I'm biulding a django project with jinja2 dealing with templates. Some page contents are submited by the client with wysiwy editor, and thing's going fine with the detail pages.

But the list pages are wrong with the slice of the contents.

My code:

<div class="summary ">
     <div class="content">{{ question.content[:200]|e}}...</div> 
</div>

But the output is:

<p>what i want to show here &nbsp;is raw text without markups</p>...

The expected result is that the html markups like <p></p> <section>.... are gone (filtered or eliminated) and only the raw text shows!

So how can I fix it? Thanks in advance!

like image 738
Foxjack Avatar asked Oct 30 '25 22:10

Foxjack


1 Answers

Use striptags filter:

striptags(value)

Strip SGML/XML tags and replace adjacent whitespace by one space.

<div class="content">{{ question.content|striptags}}...</div> 

Jinja2 striptags filter test will also help you to understand how it works.

Hope that helps.

like image 108
alecxe Avatar answered Nov 02 '25 23:11

alecxe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!