Question about Django template: how to output only text if there is html in the variable?

I have many variables that have html in them. For example, the value of a variable called {{object.name}} is as follows:

Play this <a href="#">hot</a> game and see how <b>fun</b> it is! 

Is there a filter that can be applied to a variable that will give me only text:

 Play this hot game and see how fun it is! 

Without related text or replacing html with htmlentities. Just text?

+4
source share
2 answers

striptags filter removes all html

{{object.name | striptags}}

+7
source

You have 3 options to remove the html code:

Using the safe "filter in your template:

 {{ object.name|safe }} 

Using the autoescape tag in your template:

 {% autoescape off %} {{ object.name }} {% endautoescape %} 

or by declaring it as " safe " in your Python code:

 from django.utils.safestring import mark_safe name = mark_safe(name) 
+1
source

All Articles