I've written a custom template tag, but I get the error:
'reservationtags' is not a valid tag library: Could not load template
library from django.templatetags.reservationtags, No module named
reservationtags
I've looked in the archives and have tried everything I know so far:
1. templatetags directory is in the same level as models.py
2. templatetags has two files: __init__.py and reservationtags.py
3. I ran python manage.py shell, and executed:
from pennysaver.reservations.templatetags import reservationtags
and received no error.
4. reservationtags.py has: register = template.Library() and
register.tag('columnize', do_columnize)
For good measure, here's my code:
from django import template
register = template.Library()
def do_columnize(parser, token):
nodelist = parser.parse(('endcolumnize',))
parser.delete_first_token()
#Get the number of columns, default 1
try:
tag_name, columns, row_class, cell_class =
token.contents.split(None, 3)
except ValueError:
columns = 1
return ColumnizeNode(nodelist, columns)
class ColumnizeNode(template.Node):
def __init__(self, nodelist, columns):
self.nodelist = nodelist
self.columns = columns
self.counter = 1
def render(self, context):
self.counter += 1
if (self.counter > self.columns):
self.counter = 1
if (self.counter == 1):
output = '<tr'
if self.row_class:
output += ' class="%s">' % self.row_class
else:
output += '>'
output += '<td'
if self.cell_class:
output += ' class="%s">' % self.cell_class
else:
output += '>'
output += self.nodelist.render(context)
if (self.counter == self.columns):
output += '</tr>'
return output
register.tag('columnize', do_columnize)
Thanks for any help!
Corey
>
> Hi All!
>
> I've written a custom template tag, but I get the error:
> 'reservationtags' is not a valid tag library: Could not load template
> library from django.templatetags.reservationtags, No module named
> reservationtags
>
> I've looked in the archives and have tried everything I know so far:
>
> 1. templatetags directory is in the same level as models.py
> 2. templatetags has two files: __init__.py and reservationtags.py
> 3. I ran python manage.py shell, and executed:
> from pennysaver.reservations.templatetags import reservationtags
> and received no error.
> 4. reservationtags.py has: register = template.Library() and
> register.tag('columnize', do_columnize)
The error is probably not in your module (note in the error, django
is appending 'django' to its search path), but in the {% load %} tag
in your template. You didn't specify what your template looks like.
When you code the load, do not include 'templatetags' in the path, so
in your template the load statement should look something like
{% load reservations.reservationtags %}
Don
I was SURE that I put it there! Thanks for making me look ivan!
Corey