Home Ask Login Register

Developers Planet

Your answer is one click away!

farhawa February 2016

Django, html tags not interpreted

I am loading a text from the DB of a django project to display it in the template.

In the text stored within the data base, I added some HTML tags but it seems like the browser cannot interprete this html tags.

Here is my template:

  <!-- body_text = "<strong>Hello</strong> word"  -->
  {{ entry.body_text }} 

And I got the raw text: <strong>Hello</strong> word instead of

<strong>Hello</strong> word

What I am doing wrong?


Shang Wang February 2016

If you don't want your HTML to be escaped, use safe filter:

{{ entry.body_text|safe }}

django doc about safe filter.

Sнаđошƒаӽ February 2016

You can also try this:

{% autoescape off %}
    {{ your_html_content }}
{% endautoescape %}

From django docs for autoescape:

Controls the current auto-escaping behavior. This tag takes either on or off as an argument and that determines whether auto-escaping is in effect inside the block. The block is closed with an endautoescape ending tag.

When auto-escaping is in effect, all variable content has HTML escaping applied to it before placing the result into the output (but after any filters have been applied). This is equivalent to manually applying the escape filter to each variable.

As pointed out in the other answer, you can also use safe filter which:

Marks a string as not requiring further HTML escaping prior to output. When autoescaping is off, this filter has no effect.

See here: safe filter.

Read more about django template tags and filters: Django Built-in template tags and filters

Post Status

Asked in February 2016
Viewed 2,767 times
Voted 4
Answered 2 times


Leave an answer

Quote of the day: live life