74

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.

10 Answers 10

155

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

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

will be rendered as:

{{ var }}
2
  • 2
    This is the most straight-forward way. +1
    – ProfNandaa
    Dec 2, 2015 at 18:21
  • 3
    While 'verbatim' is generally useful, Django bug code.djangoproject.com/ticket/23424 makes it problematic for this specific usage. For example this fails: {% verbatim %}{{{%endverbatim%}hello{% verbatim%}}}{% endverbatim %} The word 'verbatim' will actually appear in the rendered text... Jan 16, 2016 at 21:21
67

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>
1
  • I have two questions: 1. Why django doc don't mentions templatetag in limitations-with-string-literals; 2. What can I do if I want to escape with template filter?
    – Cloud
    May 8, 2017 at 0:30
28

Edit: I don't really recommended this because it's not very clean, but it's still an option.

I was searching for one that I could use with JQuery Templates and figured a way to do it without tags or filters. This is as short as I could get it:

{{ "{{ any text }" }}}

Is printed as:

{{ any text }}

Why it works? Any text within {{}} is displayed as is, as long as it doesn't have two closing braces }} in a row. Then there are three brackets in a row, django interprets two first ones as end of the variable leaving one additional closing brace.

15

You can try escaping with html character escapes like:

{ = &#123;

} = &#125;

<p>"&#123;&#123; some text &#125;&#125;"</p>

Update

In case anyone is trying to use the actual tags for javascript, verbatim is a better solution:

Stops the template engine from rendering the contents of this block tag.

{% verbatim %}
    {{if dying}}Still alive.{{/if}}
{% endverbatim %}
7

if you simply need to use {{ }} as a variable for template framework like angularjs, then following maybe simpler:

in your <app path>/templatetags/ngvar.py , add

from django import template
register = template.Library()

@register.simple_tag
def ngvar(var_name):
    return "{{%s}}" % var_name

and in template, do

{% load ngvar %}
{% ngvar "variable name" %}

if ngvar.py is the first template tag, then make sure to add __init__.py file to the templatetags directory

3

Another option would be to add a word joiner (zero width no-break space) between each curly bracket:

<p>"{&#8288;{ some text }&#8288;}"</p>
3

Although the above answers can solve the original problem, I add some hack around here for those who are scratching their heads like me.

Some times, we want to render a single brace followed by a variable. For example, in BibTeX, there may be something look like this:

@MISC{hu2012-spectral,
    author = {Hu, Pili},
    title = {Spectral Clustering Survey},
    howpublished = {GitHub, https://github.com/hupili/tutorial/tree/master/spectral-clustering},
    month = {May},
    year = {2012}
}

Those bib fields come from template variables. If you write

title = {{{title}}},

jinja can not compile and raise an error. If you write

title = { {{title}} },

there will be extra blanks. The hack around is to store '{' and '}' as variables and use later.

{% set lb = '{' %}
{% set rb = '}' %}
...
@MISC{{lb}}{{ meta.bib_key }},
    author = {{lb}}Hu, Pili{{rb}},
    title = {{lb}}{{ meta.title }}{{rb}},
    howpublished = {{lb}}GitHub, https://github.com/hupili/tutorial/tree/master/{{ auto.path}}{{rb}},
    month = {{lb}}{{ meta.month }}{{rb}},
    year = {{lb}}{{ meta.year }}{{rb}}
}

This looks clumsy but it is the best I find so far. If you have a cleaner solution, please tell me.

2
  • Note that the HTML entity mentioned above does not apply here. We want the raw text '{' in BiBTeX DB, instead of a '{' viewed from browser.
    – Pili Hu
    Jun 16, 2013 at 7:52
  • 1
    Nice - I made a property in my Model where I return the variable as a latexconform string e.g. return '{Author}' Oct 14, 2015 at 10:16
2

This template tag (designed for use with jQuery Templates) might do the trick. It let's you wrap content you don't want Django to interpret as variables with a template tag.

2

it can be solved by avoing adjacent angular backets, if its inside javascript code then you can write

'{'+'{address.'+key+'}}'

I used this to print jinja variables into another template,using javascript.

1

Jinja, which is what is being used for the templates, offers several suggestions for escaping here. What has worked best for me is using something like "{% raw %}{{ some text }}{% endraw %}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.