farhawa
farhawa

Reputation: 10417

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:

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

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

<strong>Hello</strong> word

What I am doing wrong?

Upvotes: 1

Views: 1873

Answers (2)

Sнаđошƒаӽ
Sнаđошƒаӽ

Reputation: 17612

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

Upvotes: 2

Shang Wang
Shang Wang

Reputation: 25559

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

{{ entry.body_text|safe }}

django doc about safe filter.

Upvotes: 7

Related Questions