Keeping Template from escaping & to &amp
Asked Answered
Q

3

7

In my Django Templates, I want to pass in a string as follows:

trademard = "MyCompany ™"

and it will show up as the correct HTML entity of TM

However, when it is passed in, ™ is turned into ™

How can I get my desired results w/o surrounding the html block w/ {% verbatim %}

Thanks

Quagga answered 30/6, 2014 at 19:44 Comment(1)
{{trademard|safe}} ??Rage
Q
5

I found my answer:

from django.utils.safestring import mark_safe
mystring = mark_safe("MyCompany ™")

works great!

Quagga answered 30/6, 2014 at 21:18 Comment(0)
Q
10

An alternative method, as @karthikr said is to use the |safe filter at the template level:

{{ trademard|safe }}

Obviously, be careful in using either of these methods -- if the data comes from a user-entered source you could cause the introduction of HTML errors or even security vulnerabilities to your application.

The official documentation has a detailed explanation of HTML escaping that all Django developers should spend time understanding.

Quirt answered 30/6, 2014 at 23:30 Comment(0)
Q
5

I found my answer:

from django.utils.safestring import mark_safe
mystring = mark_safe("MyCompany ™")

works great!

Quagga answered 30/6, 2014 at 21:18 Comment(0)
K
0

I know this is an older post, but I had to search in many places to find out a solution that does not depend on the safe tag. Maybe this can help someone:

{% autoescape off %}
{{trademard}}
{% endautoescape %}

This disables the automatic html escaping. According to the documentation the result is the same as applying the safe tag, but can be more useful when dealing with a bigger chunk of data.

Kashakashden answered 18/1 at 13:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.