0

I currently have a variable that contains a string HTML which resembles this

myvar = "<p style="-qt-block-indent: 0; text-indent: 0px; margin: 18px 0px 12px 0px;"><span style="font-size: xx-large; font-weight: 600; color: #5e9ca0;"> ..."

I am passing this string to my template like so

      return render(request, "rendered.html", {
            'result': myvar,
        })

In the template I am simply doing

{{myvar}}

This shows me on the screen the exact html as text but not rendered. When I investigated the source this is what i got

 &lt;p style=&quot;-qt-block-indent: 0; text-indent: 0px; margin: 18px 0px 12px 0px;&quot;&gt;&lt;span style ...

while I was suppose to get

<p style="-qt-block-indent: 0; text-indent: 0px; margin: 18px 0px 12px 0px;"><span style="font-size: xx-large; font-weight: 600; color: #5e9ca0;"> 

Any solution on how I can fix this issue ?

1
  • Use {{ myvar|safe }} Commented Oct 10, 2016 at 3:27

1 Answer 1

2

What is happening ?

Django by default is escaping your html thinking that it might be harmful hence escaping it by default.

Since you need to NOT escape it. Wrap your variable within autoescape filter

    {% autoescape off %}
    {{ myvar}}
    {% endautoescape %}
Sign up to request clarification or add additional context in comments.

1 Comment

Happy to help you :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.