How to output raw html text with Django template

By default, Django escapes or quotes all the text outputs in the template because of the security matter. It means that if Django template will render any html tag as text, not as html tag. But, if you want, you can display those html outputs as html tags using the Django built-in template tags: safe

For example, I will return this object to the template with:

myobject.mytextfield = '<strong>This is my text</strong>'

+ Without safe filter:

{{ myobject.mytextfield  }}

will be rendered as:

"<strong>This is my text</strong>"

+ With safe filter:

{{ myvar.mytextfield|safe }}

will be rendered as:

"This is my text"

Reference: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#safe

Comments