ikravets
ikravets

Reputation: 15

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.

Upvotes: 1

Views: 253

Answers (2)

Mariusz Jamro
Mariusz Jamro

Reputation: 31663

You're using the {% load %} syntax in a wrong way. Accoring to the doc the {% load foo from bar %} loads tag or filter named foo from tag library called bar. In your case {% load test_tag from _project_.apps.newapp.templatetags %} the test_tag is a name of the library instead of a tag or filter name.

So it should be more like:

{% load lower from test_tag  %}

Upvotes: 1

Vahid Chakoshy
Vahid Chakoshy

Reputation: 1527

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

{% load test_tag %}

Upvotes: 1

Related Questions