1

文件结构:

_project_
   __init__.py
   settings/ 
       __init__.py
       settings.py
   apps/
       __init__.py
       newapp/
           __init__.py
           models.py
           ....
           templatetags/
               __init__.py
               test_tag.py

...
__init__.py
manage.py

test_tag.py 包含:

from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()

@register.filter
@stringfilter
def lower(value):
    return value.lower()

test.html 包含:

{% load test_tag from _project_.apps.newapp.templatetags %}

Django 1.5 外壳(python manage.py 外壳):

(InteractiveConsole)
>>> from _project_.apps.newapp.templatetags import test_tag
>>> test_tag.lower("QWERTY")
u'qwerty'

Django 1.5 的设置:

INSTALLED_APPS = (
    ...
    '_project_.apps.newapp',
    ...
)

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

但是 Django 1.5 会产生异常 TemplateSyntaxError:

'_project_.apps.newapp.templatetags' is not a valid tag library: Template library _project_.apps.newapp.templatetags not found, tried ...

PS:服务器重启,*.pyc 文件被删除,但问题存在。当“newapp”位于 / project /newapp/ 时 - 一切正常。

4

2 回答 2

1

我认为你必须在你的 html 页面中修复加载模板标签

{% load test_tag %}
于 2013-03-03T20:45:34.653 回答
1

{% load %}以错误的方式使用语法。根据文档,从{% load foo from bar %}名为. 在您的情况下,这是库的名称,而不是标签或过滤器名称。foobar{% load test_tag from _project_.apps.newapp.templatetags %}test_tag

所以它应该更像:

{% load lower from test_tag  %}
于 2013-03-03T20:46:45.710 回答