admin管理员组

文章数量:1431426

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

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?

Share Improve this question asked Feb 8, 2016 at 18:51 farhawafarhawa 10.4k16 gold badges57 silver badges92 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

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

{{ entry.body_text|safe }}

django doc about safe filter.

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

本文标签: javascriptDjangohtml tags not interpretedStack Overflow