I am trying to figure out templatetags, and I keep getting the
following error:
'get_latest' is not a valid tag library: Could not load template
library from django.templatetags.get_latest, No module named
get_latest
I started with this guide:
http://www.b-list.org/weblog/2006/jun/07/django-tips-write-better-template-tags/
I have the file saved as "get_latest.py" in the "templatetags"
directory, which is in my "blog" app. I have set the blog app as an
installed app in the settings.
Here is the code that I am using:
from django.template import Library, Node
from django.db.models import get_model
register = Library()
class LatestContentNode(Node):
def __init__(self, model, num, varname):
self.num, self.varname = num, varname
self.model = get_model(*model.split('.'))
def render(self, context):
context[self.varname] = self.model._default_manager.all()
[:self.num]
return ''
def get_latest(parser, token):
bits = token.contents.split()
if len(bits) != 5:
raise TemplateSyntaxError, "get_latest tag takes exactly four
arguments"
if bits[3] != 'as':
raise TemplateSyntaxError, "third argument to get_latest tag
must be 'as'"
return LatestContentNode(bits[1], bits[2], bits[4])
get_latest = register.tag(get_latest)