DJ_help
DJ_help

Reputation:

Display Django Code from a Django template

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.

Upvotes: 1

Views: 2438

Answers (4)

Mejmo
Mejmo

Reputation: 2593

use verbatim template tag

{% verbatim %} 
...
{% endverbatim %}

Upvotes: 0

Andre Miller
Andre Miller

Reputation: 15523

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

Upvotes: 6

Stephen Van Dahm
Stephen Van Dahm

Reputation: 101

Django has a special template tag for this purpose.

Upvotes: 1

AbstractProblemFactory
AbstractProblemFactory

Reputation: 9811

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

Upvotes: 1

Related Questions