Question

I am trying to display Django source code from a Django template. However, I cannot find a tag similar to HTML's pre or xmp.

Here's the code

Also, I have a block with the same name which springs the error.

Was it helpful?

Solution

If your view puts the source code in a context variable called source, your template might look like this:

<pre>
{{ source|escape }}
</pre>

The escape filter will escape certain characters to make sure the HTML is rendered correctly.

If you just want to display hard coded template source in your template, there are two options.

Use HTML escaping to do so and remove your XMP tags.

&#123; instead of }
&#125; instead of {

Or use the templatetag template tag:

{% templatetag openbrace %} instead of }
{% templatetag closebrace %} instead of {

etc.. refer to link

OTHER TIPS

i don't really sure if i understand: If you want show django template code try change '{' and '}' to

&#123; and &#125;

After that django will not recognize it as var.

EDIT: another way to tell django not to parse code is here :) http://docs.djangoproject.com/en/dev/ref/templates/builtins/#templatetag

Django has a special template tag for this purpose.

use verbatim template tag

{% verbatim %} 
...
{% endverbatim %}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top