Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Unable to load template tags

I'm using Django 1.2.1 and I'm having problems trying to load my template tags:

{% load mytags %}

TemplateSyntaxError at /myapp/

'mytags' is not a valid tag library: Template library mytags not
found, tried django.templatetags.mytags

It's defined in myproject/myapp/templatetags/mytags.py.

nate@nate-desktop:~/work/django-projects/myproject$ ls myapp/templatetags/
mytags.py  __init.py__
nate@nate-desktop:~/work/django-projects/myproject$ more
myapp/templatetags/mytags.py


from django import template

register = template.Library()

@register.simple_tag
def myclass(request):
    return request.path

I added 'myapp' to INSTALLED_APPS, and updated TEMPLATE_LOADERS (as per a suggestion from StackOverflow):

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

When I start the server I see this warning message:

/usr/local/lib/python2.6/dist-packages/django/template/loaders/eggs.py:4:
UserWarning: Module _mysql was already imported from
/usr/lib/pymodules/python2.6/_mysql.so, but
/usr/lib/pymodules/python2.6 is being added to sys.path

When I try to import my module in the shell I also am unable to import it:

>>> from django.templatetags import mytags
Traceback (most recent call last):
 File "<console>", line 1, in <module>
ImportError: cannot import name mytags
>>> from myapp.templatetags import mytags
Traceback (most recent call last):
 File "<console>", line 1, in <module>
ImportError: No module named templatetags

Does this mean something is wrong with my path or setup? Any ideas?

like image 665
Nate Reed Avatar asked Sep 10 '25 02:09

Nate Reed


1 Answers

A note for others who run into this: you need to restart the development server to register new files in your django app.

like image 144
starsinmypockets Avatar answered Sep 12 '25 17:09

starsinmypockets