Django-您忘记注册或加载此标签了吗? [英] Django - Did you forget to register or load this tag?

查看:793
本文介绍了Django-您忘记注册或加载此标签了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个我想使用的自定义标签,但是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时,出现错误:

第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 %}

这篇关于Django-您忘记注册或加载此标签了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆