views:

51

answers:

2

How do you print out "{{text}}" in a Django template? If I type it into a Django html template it gets interpreted as the variable text. I want the actual text:

{{text}}

To appear in the html output.

+4  A: 

The best way is to use the templatetag tag. However, if i recall correctly, using {{ "{{text}}" }} will also work, this is however undocumented behaviour, so there is no real guarantee this will never break.

KillianDS
Why do I have downvotes? I gave the correct answer.
KillianDS
+4  A: 

To output the characters used to compose template tags, you have to use a specific template tag called templatetag. If you want to output the {{ characters, for example, you use {% openvariable %} and the output of that template tag would be {{.

So for your example,

{% openvariable %} text {% closevariable %}

would output:

{{ text }}
vitorbal
vitorbal has the correct, book answer. If you're going to do this a lot and want to reduce the number of (template) characters involved, you could write a template context processor that adds two bindings, "odb" (open double-brace) for "{{" and "cdb" for "}}". This lets you write `{{odb}}text{{cdb}}`.
Dave W. Smith