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?
A note for others who run into this: you need to restart the development server to register new files in your django app.
My problem was due to a typo. I called the file in templatetags __init.py__
but it should have been named __init__.py
.
I had the same issue. The problem was that I was using a shared templatetags
directory, and that didn't have the __init__.py
in it. Added that empty file in, restarted server, and all was well.
精彩评论