2015-11-23 160 views
0

我在自己的Django 1.8项目中使用自定义模板标签时遇到问题。这是发生了什么:Django自定义模板标签无法加载

TemplateSyntaxError at/
Invalid block tag: 'custom_foo' 
Request Method: GET 
Request URL: <...> 
Django Version: 1.8.3 
Exception Type: TemplateSyntaxError 
Exception Value:  
Invalid block tag: 'custom_foo' 

我的文件夹的样子:

my_app 
|---templatetags 
    |---__init__.py 
     myapp_extras.py 

而且myapp_extras.py

from django import template 
register = template.Library() 

@register.filter 
def custom_foo(): 
    return 'bar' 

我使用PyCharm5发展。 必须缺少一些东西。

我的base.html模板的顶部有{% load myapp_extras %}

回答

1

您已经注册custom_foo()作为过滤器,尝试注册它作为一个标签根据:https://docs.djangoproject.com/en/1.8/howto/custom-template-tags/,例如:

from django import template 
register = template.Library() 

class BarNode(template.Node): 
    def render(self, context): 
    return 'bar' 

@register.tag 
def custom_foo(parser, token): 
    return BarNode() 
+0

帮助。但'render()只需要1个参数(2给定)',所以'def render(self,somevar)'。现在我必须弄清楚为什么(或重写)django_hijack这样的标签工作。这是1.8的最近变化吗? –

+0

我看到它是什么。 django-hijack在他们的文档中有错误。我得到的错误是对过滤器使用标记实现的结果,这不起作用。谢谢@Adrian! –