【9】网站搭建:博客统计排行
一、前言
在阅读计数功能之后,就可以可根据每篇博客的阅读量来对博客进行热门统计排行了,如阅读周榜,月榜,总榜。基本上只要实现其中一个,其他两个也能照着做出来,大体上的逻辑是一样的。都是通过django自带的工具包中的timezone模块获取今天的日期格式,再通过datetime模块的timedelta方法来做日期的差值,然后筛选出这两个时间点之间发表的文章,除了总榜只需要筛选出日期小于今天发表的文章。将该时间段的博客列表筛选出来之后,通过聚合函数求出每篇文章的阅读量总和,然后进行阅读量的排序
二、周榜
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import datetime from django.utils import timezone from django.db.models import Sum from blog.models import Post def get_7_days_read_posts(): """ 作用:获取阅读量周榜博客榜单 :return: 周榜博客前15条博客 """ today = timezone.now().date() date = today - datetime.timedelta(days = 7 ) posts = Post.objects \ . filter (Q(display = 0 ) | Q(display__isnull = True ), read_detail__date__lt = today, read_detail__date__gte = date) \ .values( 'id' , 'title' ) \ .annotate(read_num_sum = Sum ( 'read_detail__read_num' )) \ .order_by( '-read_num_sum' ) return posts[: 15 ] |
三、月榜
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import datetime from django.utils import timezone from django.db.models import Sum from blog.models import Post def get_30_days_read_posts(): """ 作用:获取阅读量月榜博客榜单 :return: 月榜博客前15条博客 """ today = timezone.now().date() date = today - datetime.timedelta(days = 30 ) posts = Post.objects \ . filter (Q(display = 0 ) | Q(display__isnull = True ), read_detail__date__lt = today, read_detail__date__gte = date) \ .values( 'id' , 'title' ) \ .annotate(read_num_sum = Sum ( 'read_detail__read_num' )) \ .order_by( '-read_num_sum' ) return posts[: 15 ] |
四、总榜
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import datetime from django.utils import timezone from django.db.models import Sum from blog.models import Post def get_all_read_posts(): """ 作用:获取阅读量总榜博客榜单 :return: 总榜博客前15条博客 """ today = timezone.now().date() posts = Post.objects \ . filter (Q(display = 0 ) | Q(display__isnull = True ), read_detail__date__lt = today) \ .values( 'id' , 'title' ) \ .annotate(read_num_sum = Sum ( 'read_detail__read_num' )) \ .order_by( '-read_num_sum' ) return posts[: 15 ] |
五、最新发表
1 2 3 | from blog.models import Post new_publish = Post.objects. all ()[: 15 ] |
六、最新推荐
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import datetime from django.utils import timezone from .models import ReadDetail def get_new_recommend_post(content_type): """ 作用:获取最新推荐博客列表 :param content_type: 数据表的模型类 :return: 最新推荐的前15条博客 """ today = timezone.now().date() yesterday = today - datetime.timedelta(days = 1 ) read_detail = ReadDetail.objects. filter (content_type = content_type, date = yesterday).order_by( '-read_num' ) return read_detail[ 0 : 15 ] # 前十五条 |
七、随机推荐
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import random from blog.models import Post def get_random_recomment(): """ 作用:获取随机推荐博客列表 :return: 随机推荐的前15条博客 """ # 随机推荐 random_posts = set () post_list = Post.objects. filter (Q(display = 0 ) | Q(display__isnull = True )) if post_list.count() < 15 : return post_list while random_posts.__len__() < 15 : random_posts.add(random.choice(post_list)) return random_posts |
原文出处:https://jzfblog.com/detail/65,文章的更新编辑以此链接为准。欢迎关注源站文章!
【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
【推荐】2025 HarmonyOS 鸿蒙创新赛正式启动,百万大奖等你挑战
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有调度器的协程不是好协程,零基础深入浅出 C++20 协程
· 别做抢活的导演:代码中的抽象层次原则
· 从 Redis 客户端超时到 .NET 线程池挑战
· C23和C++26的#embed嵌入资源指南
· 「EF Core」框架是如何识别实体类的属性和主键的
· 博客园众包线下沙龙第1期:云栖开发者基地,共建技术新天地
· 别做抢活的导演:代码中的抽象层次原则
· .NET周刊【7月第1期 2025-07-06】
· Claude Code如何集成到VSCode、PyCharm IDE及使用技巧
· 基于外卖系统的RBAC实现