Skip to content

Instantly share code, notes, and snippets.

@jaymzcd
Created August 11, 2010 16:48
Show Gist options
  • Save jaymzcd/519297 to your computer and use it in GitHub Desktop.
Save jaymzcd/519297 to your computer and use it in GitHub Desktop.
@register.tag
def populate_posts(parser, token):
try:
tag_name, num_posts, as_name, name = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError("not enough arguments given")
return PostsNode(num_posts, name)
class PostsNode(template.Node):
def __init__(self, num_posts, name):
self.num_posts = num_posts
self.name = name
def render(self, context):
""
context[self.name] = Post.objects.filter(post_type="post",
post_status="publish").order_by("-post_date")[:self.num_posts]
return ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment