Display Django Code from a Django template

馋奶兔 提交于 2019-12-08 04:58:49

问题


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.


回答1:


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




回答2:


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




回答3:


Django has a special template tag for this purpose.




回答4:


use verbatim template tag

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


来源:https://stackoverflow.com/questions/1432439/display-django-code-from-a-django-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!