How to avoid {{or}} in a django template?

Django considers {{ var }} as some variable in its template. How can I avoid {{ var }} or {{ or }} so that django does not treat it as a variable.

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

+43
python django
Oct 14 '11 at 18:33
source share
10 answers

I believe you are looking for the templatetag tag.

As states associated with the doc,

Since the template system does not have the concept of “escaping,” you must use the {% templatetag %} tag to display one of the bits used in the template tags.

For example:

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

will appear like this:

 <p>"{{ some text }}"</p> 
+49
Oct. 14 '11 at 18:50
source share

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

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

will display as:

 {{ var }} 
+102
Jan 10 '13 at 13:40
source share

Edit: I really do not recommend this because it is not very clean, but it is still an option.

I was looking for the one I could use with jQuery templates, and decided how to do this without tags or filters. This is as short as possible than I could:

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

Printed as:

 {{ any text }} 

Why does it work? Any text inside {{}} is displayed as is if it does not have two closing brackets}} in the line. Then there are three brackets in a row, django interprets the first two as the end of the variable, leaving one additional closing figure.

+20
Apr 11 2018-12-12T00:
source share

You can try escaping using html characters, for example:

{= &#123;

} = &#125;

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

Try to do this in your browser.

+10
Oct. 14 '11 at 18:54
source share

if you just need to use {{}} as a variable for the template structure, for example angularjs, then maybe simpler:

in <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 the do pattern

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

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

+7
Oct 27 '14 at 17:30
source share

Although the above answers may solve the original problem, I add some hacks here for those who scratch their heads like me.

Several times we want to display a single bracket followed by a variable. For example, in BibTeX there might be something similar to 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} } 

These bib fields come from template variables. If you write

 title = {{{title}}}, 

jinja cannot compile and raise an error. If you write

 title = { {{title}} }, 

there will be extra spaces. Clap around - 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}} } 

It looks awkward, but it's the best I find so far. If you have a cleaner solution, please tell me.

+3
Jun 16 '13 at 7:51 on
source share

Another option would be to add a word joiner (zero width without space) between each brace:

 <p>"{&#8288;{ some text }&#8288;}"</p> 
+2
Oct 14 '11 at 19:30
source share

This template tag (designed for use with jQuery templates) can do the trick. This allows you to wrap content that you do not want Django to interpret as variables with a template tag.

+2
Oct. 15 '11 at 19:43
source share

Jinja, which is used for templates, offers some screening suggestions here. What works best for me is to use something like "{% raw %}{{ some text }}{% endraw %}"

+1
Jul 11 '14 at 14:15
source share

it can be resolved by using adjacent angular edges if its internal javascript code you can write

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

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

0
Oct 30 '15 at 5:17
source share



All Articles