Search code examples
djangodjango-templatesdjango-custom-tags

Django loading templatetags from nested apps doesn't working correctly


File structure:

_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 contains:

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

register = template.Library()

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

test.html contains:

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

Django 1.5 Shell ( python manage.py shell ):

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

Django's 1.5 settings:

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

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

But Django 1.5 generates exception TemplateSyntaxError:

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

P.S: Server restarted, *.pyc files removed, but problem exists. When 'newapp' located in /project/newapp/ - all OK.


Solution

  • i think you must be fix load templat tag in your html page

    {% load test_tag %}