How to escape {{ or }} in django template?


Question

Django treats {{ var }} as some variable in its template. How can I escape {{ var }} or {{ or }} such that django does not treat it as variable.

<p>"{{ some text }}"</p> Should prints exactly the same.

1
58
10/14/2011 6:33:13 PM

Accepted Answer

I believe you are looking for the templatetag template tag.

As the linked-to doc states,

Since the template system has no concept of "escaping", to display one of the bits used in template tags, you must use the {% templatetag %} tag.

For example:

<p>"{% templatetag openvariable %} some text {% templatetag closevariable %}"</p>

will appear as so:

<p>"{{ some text }}"</p>
60
10/14/2011 6:50:42 PM

Django 1.5 introduced {% verbatim %} template tag. It stops template from parsing contents of this tag:

{% verbatim %}
    {{ var }}
{% endverbatim %}

will be rendered as:

{{ var }}

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon