views:

393

answers:

3

Hi All,

I am currently writing a few custom template tags but for some reason they will not load. My directory structure is as follows:

MyProj
 |
 ----MyApp
     |
     |----templatetags
          |
          |----myapp_tags.py
          |----__init__.py

In myapp_tags.py

from django.template import Library, Node
from myproj.myapp.models import Product

register = Library()

class LatestProductsNode(Node):
    def render(self, context):
        context['recent_products'] = Product.objects.all()[:5]
        return ''

def get_latest_products(parser, token):
    return LatestProductsNode()

get_latest_products = register.tag(get_latest_products)

In settings.py

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',
    'myproj.myapp',
)

In the Template

{% load myapp_tags %}

The error i get when trying to load the page:

Exception Type:     TemplateSyntaxError Exception Value:  

'myapp_tags' is not a valid tag library: Could not load template library from django.templatetags.myapp_tags, No module named myapp_tags
A: 

The problem is that nyapp_tags is not at the top level of an installed project. If you put myproj.myapp.templatetags in INSTALLED_APPS, you should be fine.

David Berger
`INSTALLED_APPS` is not `sys.path`, `myproj.myapp.templatetags` is not valid app.
PiotrLegnica
Hi, This seems to work. Though is this a bug? or is this how it's supposed to work? From the documentation i understand that django loops through installed apps and looks for a directory called 'templatetags' within any of the installed apps. Therefore you should not have to specifically add this!
izzy
Issy, you're correct. You shouldn't put the templatetags folder on the python path. Often if the template tag can't be found, there's an import error occurring - but it doesn't seem obvious from your code (perhaps a circular import?)
SmileyChris
+3  A: 

in settings.py, you should never name the project 'myproj' explicitely. In INSTALLED_APPS, just use 'myapp'. Also, you should have this :

TEMPLATE_LOADERS = (
    'django.template.loaders.app_directories.load_template_source',
)

And be sure to have an __init__.py in the myapp folder.

Use manage.py shell then from myapp.templatetags import myapp_tags to find out if theres any python error in the myapp_tags.py file.

Also, be sure that myapp_tags.py file name doesnt conflicts with another folder/file in your project.

Hope this helps.

jujule
+1  A: 

One thing that's tripped me up is that the magic importing of templatetags bypasses the automatic reloading of the development server. If the following works in manage.py shell

>>> from django.templatetags import myapp_tags
>>>

Then everything is actually working and you just need to reload the development server. If on the other hand you get an ImportError then something is wrong and you should check your INSTALLED_APPS, that you have an __init__.py file in the templatetags directory and all the other things suggested in the other answers.

This will probably only apply to a tiny fraction of the people who experience template tag loading problems, but this is the second time I've arrived at this question in as many weeks and both times it's just taken restarting the development server to get things working.

Dan Head
Thanks, this was my issue - was pulling my hair out!
John McCollum