Created
August 11, 2010 16:48
-
-
Save jaymzcd/519297 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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