@register.as_tag
def blog_recent_posts(limit=5, tag=None, username=None, category=None):
 blog_posts = BlogPost.objects.published().select_related(u'user')
 title_or_slug = (lambda s: (Q(title=s) | Q(slug=s)))
 if (tag is not None):
  try:
   tag = Keyword.objects.get(title_or_slug(tag))
   blog_posts = blog_posts.filter(keywords__keyword=tag)
  except Keyword.DoesNotExist:
   return []
 if (category is not None):
  try:
   category = BlogCategory.objects.get(title_or_slug(category))
   blog_posts = blog_posts.filter(categories=category)
  except BlogCategory.DoesNotExist:
   return []
 if (username is not None):
  try:
   author = User.objects.get(username=username)
   blog_posts = blog_posts.filter(user=author)
  except User.DoesNotExist:
   return []
 return list(blog_posts[:limit])

@ register . as_tag def blog_recent_posts ( limit = 5 , tag = None , username = None , category = None ) : blog_posts = BlogPost . objects . published ( ) . select_related ( u'user' ) title_or_slug = ( lambda s : ( Q ( title = s ) | Q ( slug = s ) ) ) if ( tag is not None ) : try : tag = Keyword . objects . get ( title_or_slug ( tag ) ) blog_posts = blog_posts . filter ( keywords__keyword = tag ) except Keyword . DoesNotExist : return [ ] if ( category is not None ) : try : category = BlogCategory . objects . get ( title_or_slug ( category ) ) blog_posts = blog_posts . filter ( categories = category ) except BlogCategory . DoesNotExist : return [ ] if ( username is not None ) : try : author = User . objects . get ( username = username ) blog_posts = blog_posts . filter ( user = author ) except User . DoesNotExist : return [ ] return list ( blog_posts [ : limit ] )
put a list of recently published blog posts into the template context .