'get_latest' is not a valid tag library

187 weergaven
Naar het eerste ongelezen bericht

Matthew Crist

ongelezen,
25 sep 2008, 21:29:4625-09-2008
aan Django users
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)

R. Gorman

ongelezen,
25 sep 2008, 22:08:2925-09-2008
aan Django users
Post your template code as well.

R.

Matthew Crist

ongelezen,
25 sep 2008, 23:29:4425-09-2008
aan Django users
Here's my template:

{% load get_latest %}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://
www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>My Site - {%block pagetitle %}{% endblock %}</title>
</head>
<body>
<div id="header">My Site</div>
<div id="nav">
<ul>
<li><a href="/">home</a></li>
<li><a href="/blog/">Blog</a></li>
<li><a href="/links/">Links</a></li>
<li><a href="/about/">About</a></li>
</ul>
</div>
<div id="content">
<div id="primary">
<h1>{% block title %}{% endblock %}</h1>
{% block primary %}{% endblock %}
</div>
<div id="secondary">
<h3>Recent Entries:</h3>
{% get_latest blog.Entry 5 as recent_posts %}
<ul class="archive">
{% for obj in recent_posts %}
<li>
<a href="{{ obj.get_absolute_url }}" title="Permanent Link to
{{ obj.title}}">{{ obj.title}}</a>
</li>
{% endfor %}
</ul>
<div>
</div>
</body>
</html>

Rock

ongelezen,
25 sep 2008, 23:39:1825-09-2008
aan Django users
You need an empty __init.py__ in the directory or else the python
files there will not be found.

Matthew Crist

ongelezen,
26 sep 2008, 06:56:0326-09-2008
aan Django users
I've got that. Here's my directory structure:

-blog
-templatetags
-__init__.py
-get_latest.py

Rock

ongelezen,
26 sep 2008, 14:54:4826-09-2008
aan Django users

Hmmm... Here are some guesses:

If that return statement in render is a single doublequote instead of
2 singlequotes, then your code probably won't parse and, even if it
does, get_latest certainly won't be found.

It seems to me like you need to coerce the string being assigned to
self.num as make it an int. (Better yet, put that logic in the parse
section.) However that is unlikely to be the cause of the error that
you are seeing.

You might want to try compiling the code by hand to make sure there
are no stray typos.

Matthew Crist

ongelezen,
26 sep 2008, 18:10:0126-09-2008
aan Django users
It was the quotes.

Thanks for the tips!
Allen beantwoorden
Auteur beantwoorden
Doorsturen
0 nieuwe berichten