从Django模板显示Django代码 [英] Display Django Code from a Django template
问题描述
我正在尝试从Django模板中显示Django源代码。但是,我找不到类似于HTML的pre或xmp的标签。
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.
这里是代码
另外,我有一个名称相同的块,弹出错误。
Also, I have a block with the same name which springs the error.
推荐答案
如果您的视图将源代码放在名为source的上下文变量中,您的模板可能如下所示:
If your view puts the source code in a context variable called source, your template might look like this:
<pre>
{{ source|escape }}
</pre>
escape 过滤器将转义某些字符,以确保HTML正确呈现。
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.
使用HTML转义来执行此操作并删除您的XMP标签。
$ b
Use HTML escaping to do so and remove your XMP tags.
{ instead of }
} instead of {
或使用 templatetag 模板标签:
{% templatetag openbrace %} instead of }
{% templatetag closebrace %} instead of {
<请参阅链接
这篇关于从Django模板显示Django代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!