def get(self, *args, **kwargs): current_page = get_cleaned_query_data(self, [ 'page', ], blank=True)['page'] current_page = get_page_number(current_page) posts, page_number_limit = BlogPost.list_recently( page_number=current_page) for post in posts: post.labels = BlogPostLabel.get_post_label(post) pages = get_page_nav(current_page, page_number_limit, config.default_page_limit) # 使用到联合、分组等查询, 得到每个分类下文章个数, 得到每个标签下文章个数 # select category_id, blogpostcategory.name, count(blogpost.id) from blogpost inner join blogpostcategory on blogpost.category_id=blogpostcategory.id group by category_id; categorys = BlogPost.select(BlogPostCategory.name, fn.COUNT(BlogPost.id).alias('count'))\ .join(BlogPostCategory, on=(BlogPostCategory.id == BlogPost.category))\ .group_by(BlogPost.category) for category in categorys: category.name = category.category.name labels = BlogPostLabel.select( BlogPostLabel.name, fn.COUNT(BlogPostLabel.post).alias('count')).where( BlogPostLabel.is_del == False).group_by(BlogPostLabel.name) self.render('blog/index.html', posts=posts, labels=labels, categorys=categorys, pages=pages, pages_prefix_url='/blog?page=')
def get(self, category_name, *args, **kwargs): try: category = BlogPostCategory.get(BlogPostCategory.name == category_name) except BlogPostCategory.DoesNotExist: self.redirect("/static/404.html") return current_page = get_cleaned_query_data(self, ['page',], blank=True)['page'] current_page = get_page_number(current_page) posts, page_number_limit = BlogPost.list_by_category(category, page_number=current_page) for post in posts: post.labels = BlogPostLabel.get_post_label(post) pages = get_page_nav(current_page, page_number_limit, config.default_page_limit) # 使用到联合、分组等查询, 得到每个分类下文章个数, 得到每个标签下文章个数 # select category_id, blogpostcategory.name, count(blogpost.id) from blogpost inner join blogpostcategory on blogpost.category_id=blogpostcategory.id group by category_id; categorys = BlogPost.select(BlogPostCategory.name, fn.COUNT(BlogPost.id).alias('count'))\ .join(BlogPostCategory, on=(BlogPostCategory.id == BlogPost.category))\ .group_by(BlogPost.category) for category in categorys: category.name = category.category.name labels = BlogPostLabel.select(BlogPostLabel.name, fn.COUNT(BlogPostLabel.post).alias('count')).where(BlogPostLabel.is_del == False).group_by(BlogPostLabel.name) self.render('blog/index.html', posts=posts, labels=labels, categorys=categorys, pages=pages, pages_prefix_url='/blog/category/'+category_name+'?page=')
def get(self, *args, **kwargs): posts = BlogPost.select() for post in posts: post.labels = BlogPostLabel.get_post_label(post) post.category_name = post.category.name self.render('blog/post-opt.html', posts=posts)