Hi,
I followed the B-List article to write better template tags:
http://www.b-list.org/weblog/2006/06/07/django-tips-write-better-template-tags
So, I've made a blog_posts.py file :
-----------------------------------------------------------
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)
-----------------------------------------------------------
Now, inside my template "base.html", I call it like this :
{% load blog_posts %}
{% get_latest blog.Post 10 as recent_posts %}
{% for p in recent_posts reversed %}
<li><a class="lien_recents" href="{{ p.get_absolute_url }}" title="{{
p.title }}">{{ p }}</a></li>
{% endfor %}
The problem is that it's returning the first 10 blog entries, not the
10 lasts.
I've tried to change the line :
context[self.varname] = self.model._default_manager.all()[:self.num]
with
context[self.varname] = self.model._default_manager.all()[self.num:]
for exemple, but it's returning an error.
What's wrong ?
Thanks a lot.
6TooL9
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---