【django项目后台开发】数据统计——用户总数统计、日增用户数统计

from django.urls import re_path
from rest_framework_jwt.views import obtain_jwt_token
from .views import users
from .views import statistics

urlpatterns=[
    re_path('^mg\_admin/login/$',obtain_jwt_token),
    re_path('^statistics/day\_active/$',statistics.UserActiveCountView.as_view()),
]

视图

class UserActiveCountView(APIView):
	'''
 获取⽇活跃⽤户数
 '''
	#指定管理员权限
	permission_classes=IsAdminUser
	def get(self,request):
		#获取当前日期
		now_today=date.today()
		year=now_today.year
		month=now_today.month
		day=now_today.day
		# 获取当⽇登录⽤户数量 last\_login记录最后登录时间count=User.objects.filter(last\_login\_\_year=year,last\_login\_\_month=month,last\_login\_\_day=day).count()
		return Response({'count':count})

注意:需要将配置文件中的USE_TZ改为False,才能展示当前时间
在这里插入图片描述

上述前端代码实现

    data() {
      return {
        host:'http://192.168.17.129:8880',
        token:localStorage.token,
        username:localStorage.username,
        userid:localStorage.user_id,

        stat: [
          [
            
          ],
          
        ]
        
      }
    },
    computed:{
      chartLine1() {
        return this.$echarts.init(Util.getDom('line1'));
      }
    },
    methods: {
      getOrderCount(){
        this.$axios.get(this.host +'/statistics/time\_order\_count/',{
          headers:{
            'Authorization': 'JWT ' + this.token
          }
        }).then(response=>{

          this.drawLine1(response.data);

        }).catch(error=>{
          console.log(error.response);
        })
      },
      drawLine1(data)
      {
        

        let title = "今日和昨日下单量";
        let option = {
          title: Object.assign({}, Util.defaultEchartsOpt.title, {text: title}),
          grid: {
            top: 60,
            left: 60,
            right: 80,
            bottom: 20,
            containLabel: true
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              lineStyle: {
                color: '#ddd'
              }
            },
            backgroundColor: 'rgba(255,255,255,1)',
            padding: [5, 10],
            textStyle: {
              color: '#999',
            },
            extraCssText: 'box-shadow: 0 0 5px rgba(0,0,0,0.3)'
          },
          legend: {
            top: 15,
            right: 20,
            orient: 'vertical',
            textStyle: {
              color: "#666"
            }
          },
          xAxis: {
            type: 'category',
            data: ['00:00','2:00','4:00','6:00','8:00','10:00','12:00','14:00','16:00','18:00','20:00','22:00'],
            boundaryGap: false,
            splitLine: {
              show: false,
              interval: 'auto',
              lineStyle: {
                color: ['#D4DFF5']
              }
            },
            axisTick: {
              show: false
            },
            axisLine: {
              lineStyle: {
                color: '#999'
              }
            },
            axisLabel: {
              margin: 10,
              textStyle: {
                fontSize: 14
              }
            }
          },
          yAxis: {
            type: 'value',
            splitLine: {
              lineStyle: {
                color: ['#D4DFF5']
              }
            },
            axisTick: {
              show: false
            },
            axisLine: {
              lineStyle: {
                color: '#999'
              }
            },
            axisLabel: {
              margin: 10,
              textStyle: {
                fontSize: 14
              }
            }
          },
          series: [{
            name: '今日',
            type: 'line',
            smooth: true,
            showSymbol: false,
            symbol: 'circle',
            symbolSize: 4,
            data: data['t\_count\_list'],
            areaStyle: {
              normal: {
                color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                  offset: 0,
                  color: 'rgba(199, 237, 250,0.5)'
                }, {
                  offset: 1,
                  color: 'rgba(199, 237, 250,0.2)'
                }], false)
              }
            },
            itemStyle: {
              normal: {
                color: 'rgba(154, 116, 179, 0.7)'
              }
            },
            lineStyle: {
              normal: {
                width: 2
              }
            }
          }, {
            name: '昨日',
            type: 'line',
            smooth: true,
            showSymbol: false,
            symbol: 'circle',
            symbolSize: 4,
            data: data['y\_count\_list'],
            areaStyle: {
              normal: {
                color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                  offset: 0,
                  color: 'rgba(216, 244, 247,1)'
                }, {
                  offset: 1,
                  color: 'rgba(216, 244, 247,1)'
                }], false)
              }
            },
            itemStyle: {
              normal: {
                color: 'rgba(126, 237, 238, 0.7)'
              }
            },
            lineStyle: {
              normal: {
                width: 2
              }
            }
          }]
        };
        this.chartLine1.setOption(option);
        return this;
      },
      day\_active\_count(){
        // 获取日活跃用户总数
        this.$axios.get(this.host+'/statistics/day\_active/',{
          headers:{
            'Authorization': 'JWT ' + this.token
          }
        }).then(response=>{
          this.stat[0].splice(2,0,{
              title: '日活跃用户总数',
              total: response.data.count,
              bgColor: '#67c4ed'
            });
        }).catch(error=>{
          console.log(error.response);
        });

      },
      day\_increment\_count(){
        // 获取日增用户总数
        this.$axios.get(this.host+'/statistics/day\_increment/',{
          headers:{
            'Authorization': 'JWT ' + this.token
          }
        }).then(response=>{
          this.stat[0].splice(1,0,{
              title: '日增用户总数',
              total: response.data.count,
              bgColor: '#3acaa9'
            });
        }).catch(error=>{
          console.log(error.response);
        });

        
      },
      total\_user\_count(){
        // 获取用户总数
        this.$axios.get(this.host+'/statistics/total\_count/',{
          headers:{
            'Authorization': 'JWT ' + this.token
          }
        }).then(response=>{
          this.stat[0].splice(0,0,{
              title: '用户总数',
              total: response.data.count,
              bgColor: '#ebcc6f'
            });
        }).catch(error=>{
          console.log(error.response);
        });

        
      },
      


![img](https://img-blog.csdnimg.cn/img_convert/16c0d458b7166229c8003417d2d2d4a2.png)
![img](https://img-blog.csdnimg.cn/img_convert/b65609189e85574dcab5f6990e0d3407.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

5725479)]
[外链图片转存中...(img-rJlwLFXn-1714255725480)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值