文件结构:
_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/ 时 - 一切正常。