自定义标签未加载到模板中 [英] Custom tag not loaded in template
问题描述
我创建了一个我想使用的自定义标签,但是Django似乎找不到它.我的 templatetags
目录设置如下:
I've created a custom tag that I want to use, but Django can't seem to find it. My templatetags
directory is set up like this:
pygmentize.py
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from django import template
from pygments.formatters.other import NullFormatter
register = template.Library()
@register.tag(name='code')
def do_code(parser,token):
code = token.split_contents()[-1]
nodelist = parser.parse(('endcode',))
parser.delete_first_token()
return CodeNode(code,nodelist)
class CodeNode(template.Node):
def __init__(self,lang,code):
self.lang = lang
self.nodelist = code
def render(self,context):
code = self.nodelist.render(context)
lexer = get_lexer_by_name('python')
return highlight(code,lexer,NullFormatter())
我正在尝试使用此标记在 gameprofile.html
中呈现代码.
I am trying to use this tag to render code in gameprofile.html
.
gameprofile.html
(% load pygmentize %}
{% block content %}
<title>{% block title %} | {{ game.title }}{% endblock %}</title>
<div id="gamecodecontainer">
{% code %}
{{game.code}}
{% endcode %}
</div>
{% endblock content %}
当我导航至 gameprofile.html
时,出现错误消息:
When I navigate to gameprofile.html
, I get an error:
第23行上的无效块标记:代码",预期为"endblock".您忘记注册或加载此标签了吗?
Invalid block tag on line 23: 'code', expected 'endblock'. Did you forget to register or load this tag?
推荐答案
此行中的错误:(%load pygmentize%}
,无效标记.将其更改为 {%load pygmentize%}
The error is in this line: (% load pygmentize %}
, an invalid tag.
Change it to {% load pygmentize %}
这篇关于自定义标签未加载到模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!