Rendering a template variable as HTML

xpanta picture xpanta · Jan 31, 2011 · Viewed 160.5k times · Source

I use the 'messages' interface to pass messages to user like this:

request.user.message_set.create(message=message)

I would like to include html in my {{ message }} variable and render it without escaping the markup in the template.

Answer

Yuji 'Tomita' Tomita picture Yuji 'Tomita' Tomita · Jan 31, 2011

If you don't want the HTML to be escaped, look at the safe filter and the autoescape tag:

safe:

{{ myhtml |safe }}

autoescape:

{% autoescape off %}
    {{ myhtml }}
{% endautoescape %}