1.css
複雜版html
統計,經過跨表查詢和timedate模塊過濾找到前端
1 from django.db.models import Count 2 3 class TongJiView(View): 4 def today(self): 5 import datetime 6 today=datetime.datetime.now().date() 7 customer_list=Customer.objects.filter(deal_date=today) 8 9 # 查詢每個銷售的名字以及今天對應的成單量 10 ret=UserInfo.objects.filter(depart_id=2,customers__deal_date=today).annotate(c=Count("customers")).values_list("username","c") 11 print(ret) 12 ret=[[item[0],item[1]] for item in list(ret)] 13 14 return {"customer_list":customer_list,"ret":list(ret)} 15 16 def zuotian(self): 17 import datetime 18 zuotian = datetime.datetime.now().date()-datetime.timedelta(days=1) 19 customer_list = Customer.objects.filter(deal_date=zuotian) 20 21 # 查詢每個銷售的名字以及昨天對應的成單量 22 ret = UserInfo.objects.filter(depart_id=2, customers__deal_date=zuotian).annotate( 23 c=Count("customers")).values_list("username", "c") 24 print(ret) 25 print(ret) 26 ret = [[item[0], item[1]] for item in list(ret)] 27 28 return {"customer_list": customer_list, "ret": list(ret)} 29 30 31 32 def week(self): 33 import datetime 34 today = datetime.datetime.now().date() 35 weekdelta = datetime.datetime.now().date()-datetime.timedelta(weeks=1) 36 customer_list = Customer.objects.filter(deal_date__gte=weekdelta,deal_date__lte=today) 37 38 # 查詢每個銷售的名字以及昨天對應的成單量 39 ret = UserInfo.objects.filter(depart_id=2, customers__deal_date__gte=weekdelta,customers__deal_date__lte=today).annotate( 40 c=Count("customers")).values_list("username", "c") 41 print(ret) 42 43 print(ret) 44 ret = [[item[0], item[1]] for item in list(ret)] 45 46 return {"customer_list": customer_list, "ret": list(ret)} 47 48 49 def recent_month(self): 50 import datetime 51 today = datetime.datetime.now().date() 52 weekdelta = datetime.datetime.now().date()-datetime.timedelta(weeks=4) 53 customer_list = Customer.objects.filter(deal_date__gte=weekdelta,deal_date__lte=today) 54 55 # 查詢每個銷售的名字以及昨天對應的成單量 56 ret = UserInfo.objects.filter(depart_id=2, customers__deal_date__gte=weekdelta,customers__deal_date__lte=today).annotate( 57 c=Count("customers")).values_list("username", "c") 58 print(ret) 59 60 print(ret) 61 ret = [[item[0], item[1]] for item in list(ret)] 62 63 return {"customer_list": customer_list, "ret": list(ret)} 64 65 def get(self,request): 66 67 date=request.GET.get("date","today") 68 69 if hasattr(self,date): 70 context=getattr(self,date)() 71 72 return render(request,"customer/tongji.html",context)
1 <!DOCTYPE html> 2 <html lang="zh-CN"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 <meta name="viewport" content="width=device-width, initial-scale=1"> 7 <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css"> 8 9 </head> 10 <body> 11 <h3>客戶成單量統計</h3> 12 13 <hr> 14 <a href="?date=today">今天</a> 15 <a href="?date=zuotian">昨天</a> 16 <a href="?date=week">最近一週</a> 17 <a href="?date=recent_month">最近一個月</a> 18 <hr> 19 20 21 <div class="container"> 22 <div class="row"> 23 <div class="col-md-12"> 24 <table id="example2" class="text-center table table-bordered table-hover"> 25 <thead> 26 <tr> 27 <th class="text-center">編號</th> 28 <th class="text-center">客戶姓名</th> 29 <th class="text-center">性別</th> 30 <th class="text-center">客戶來源</th> 31 <th class="text-center">銷售</th> 32 <th class="text-center">所報班級</th> 33 </tr> 34 </thead> 35 <tbody> 36 37 {% for customer in customer_list %} 38 <tr> 39 <td>{{ forloop.counter }}</td> 40 <td>{{ customer.name }}</td> 41 <td>{{ customer.get_sex_display }}</td> 42 <td>{{ customer.get_source_display }}</td> 43 <td>{{ customer.consultant }}</td> 44 <td>{{ customer.get_classlist }}</td> 45 46 </tr> 47 {% endfor %} 48 49 50 </tbody> 51 </table> 52 <hr> 53 <div id="container" style="width:600px;height:400px"></div> 54 </div> 55 </div> 56 </div> 57 58 59 <script src="/static/highchart/highcharts.js"></script> 60 <script> 61 var chart = Highcharts.chart('container', { 62 chart: { 63 type: 'column' 64 }, 65 title: { 66 text: '統計成單量' 67 }, 68 subtitle: { 69 text: '數據截止 2017-03,來源: <a href="https://en.wikipedia.org/wiki/List_of_cities_proper_by_population">Wikipedia</a>' 70 }, 71 xAxis: { 72 type: 'category', 73 labels: { 74 rotation: 0 // 設置軸標籤旋轉角度 75 } 76 }, 77 yAxis: { 78 min: 0, 79 title: { 80 text: '成單數' 81 } 82 }, 83 legend: { 84 enabled: false 85 }, 86 tooltip: { 87 pointFormat: '成單人數: <b>{point.y} 單</b>' 88 }, 89 series: [{ 90 name: '總人口', 91 data: {{ ret|safe }}, 92 dataLabels: { 93 enabled: true, 94 rotation: -90, 95 color: '#FFFFFF', 96 align: 'right', 97 format: '{point.y:.1f}', // :.1f 爲保留 1 位小數 98 y: 10 99 } 100 }] 101 }); 102 103 </script> 104 </body> 105 </html>
2.簡單版django
1 from django.views import View 2 from app01.models import Customer,UserInfo 3 from django.db.models import Count 4 5 class TongJiView2(View): 6 7 def get(self,request): 8 date=request.GET.get("date","today") 9 # func=getattr(self,date) 10 #ret=func() 11 import datetime 12 now=datetime.datetime.now().date() 13 delta1=datetime.timedelta(days=1) 14 delta2=datetime.timedelta(weeks=1) 15 delta3=datetime.timedelta(weeks=4) 16 17 condition={ 18 "today":[{"deal_date__date":now},{"customers__deal_date":now}], 19 "yesterday":[{"deal_date__date":now-delta1},{"customers__deal_date":now-delta1}], 20 "week":[{"deal_date__gte":now-delta2,"deal_date__lte":now}, 21 {"customers__deal_date__gte":now-delta2,"customers__deal_date__lte":now} 22 ], 23 "recent_month":[{"deal_date__gte":now-delta3,"deal_date__lte":now}, 24 {"customers__deal_date__gte":now-delta3,"customers__deal_date__lte":now} 25 ], 26 } 27 28 customer_list=Customer.objects.filter(**(condition.get(date)[0])) 29 ret=UserInfo.objects.all().filter(**(condition.get(date)[1])).annotate(c=Count("customers")).values_list("username","c") 30 ret = [[item[0], item[1]] for item in list(ret)] 31 32 return render(request,"customer/tongji.html",locals())