@register.as_tag
def blog_categories(*args):
 posts = BlogPost.objects.published()
 categories = BlogCategory.objects.filter(blogposts__in=posts)
 return list(categories.annotate(post_count=Count(u'blogposts')))

@ register . as_tag def blog_categories ( * args ) : posts = BlogPost . objects . published ( ) categories = BlogCategory . objects . filter ( blogposts__in = posts ) return list ( categories . annotate ( post_count = Count ( u'blogposts' ) ) )
put a list of categories for blog posts into the template context .