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>
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 }}
- [Django]-Django 1.5 β How to use variables inside static tag
- [Django]-Why is __init__ module in django project loaded twice
- [Django]-Django: Grab a set of objects from ID list (and sort by timestamp)
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.
- [Django]-Is it possible to pass query parameters via Django's {% url %} template tag?
- [Django]-Why is logged_out.html not overriding in django registration?
- [Django]-How can I serialize a queryset from an unrelated model as a nested serializer?
15
You can try escaping with html character escapes like:
{ = {
} = }
<p>"{{ some text }}"</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 %}
- [Django]-Python + Django page redirect
- [Django]-Django template display item value or empty string
- [Django]-Django connection to PostgreSQL: "Peer authentication failed"
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
- [Django]-What is actually assertEquals in Python?
- [Django]-Good open source django project for learning
- [Django]-What does error mean? : "Forbidden (Referer checking failed β no Referer.):"
3
Another option would be to add a word joiner (zero width no-break space) between each curly bracket:
<p>"{⁠{ some text }⁠}"</p>
- [Django]-Django: Get an object form the DB, or 'None' if nothing matches
- [Django]-Add Text on Image using PIL
- [Django]-Why is __init__ module in django project loaded twice
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.
- [Django]-Django Rest Framework β no module named rest_framework
- [Django]-Table thumbnail_kvstore doesn't exist
- [Django]-No module named pkg_resources
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.
- [Django]-Get protocol + host name from URL
- [Django]-Github issues api 401, why? (django)
- [Django]-How to add custom field in ModelSerializer?
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.
- [Django]-How can I find the union of two Django querysets?
- [Django]-How to debug in Django, the good way?
- [Django]-Django fix Admin plural
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 %}"
- [Django]-How to save pillow image object to Django ImageField?
- [Django]-Disable session creation in Django
- [Django]-How do you insert a template into another template?