您现在的位置是:首页 > 文章详情

基于django的视频点播网站开发-step14-数据总览功能

日期:2019-02-21点击:401

数据总览功能,是对网站中产生的数据进行一个统计,统计出视频数、发布数、用户数、评论数,等等。让管理者对网站数据有一个清晰的认识,做到心中有数。

在本站中,笔者一共列举了下面几种数据:视频数、发布中
未发布、用户数、用户新增、评论数、评论新增,等几项内容。

我们把所有的数据都封装到了一个函数里面,即 IndexView 它位于后台管理的首页。

path('', views.IndexView.as_view(), name='index'),

IndexView代码如下

class IndexView(AdminUserRequiredMixin, generic.View): def get(self, request): video_count = Video.objects.get_count() video_has_published_count = Video.objects.get_published_count() video_not_published_count = Video.objects.get_not_published_count() user_count = User.objects.count() user_today_count = User.objects.exclude(date_joined__lt=datetime.date.today()).count() comment_count = Comment.objects.get_count() comment_today_count = Comment.objects.get_today_count() data = {"video_count": video_count, "video_has_published_count": video_has_published_count, "video_not_published_count": video_not_published_count, "user_count": user_count, "user_today_count": user_today_count, "comment_count": comment_count, "comment_today_count": comment_today_count} return render(self.request, 'myadmin/index.html', data)

与视频相关的统计,我们封装到了Video的models.py下面,

class VideoQuerySet(models.query.QuerySet): # 视频总数 def get_count(self): return self.count() # 发布数 def get_published_count(self): return self.filter(status=0).count() # 未发布数 def get_not_published_count(self): return self.filter(status=1).count() 

以上数据,大都使用了filter过滤器进行了过滤,最后通过count()函数返回给业务方。

与用户相关的统计,我们直接通过count和exclude将相关数据过滤出来。

与评论相关的统计,封装到了Comment的models.py下面,

class CommentQuerySet(models.query.QuerySet): # 评论总数 def get_count(self): return self.count() # 今日新增 def get_today_count(self): return self.exclude(timestamp__lt=datetime.date.today()).count()

其中,今日新增评论,我们通过exclude来过滤时间,使用了 lt 标签来过滤。更多标签的使用方法,可参考 官方文档

原文链接:https://yq.aliyun.com/articles/691087
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章