django項目用higcharts統計最近七天文章點擊量。

下載higcharts插件放在static文件夾下html

前端引入前端

<script src="/static/highcharts/highcharts.js"></script>
<script src="/static/highcharts/modules/exporting.js"></script>
<script src="/static/highcharts/modules/oldie.js"></script>
<script src="/static/highcharts/highcharts-zh_CN.js"></script>

定義在頁面中的位置app

<div id="container" style="min-width:400px;height:400px"></div>

前端jsspa

<script> var chart = Highcharts.chart('container', { chart: { type: 'line' }, title: { text: '日點擊量和點贊量統計' }, subtitle: { text: '數據來源: terroristhouse.com' }, xAxis: { categories: {{ list_week_day|safe }} }, yAxis: { title: { text: '數量 (次)' } }, plotOptions: { line: { dataLabels: { // 開啓數據標籤 enabled: true }, // 關閉鼠標跟蹤,對應的提示框、點擊事件會失效 enableMouseTracking: false } }, series: [{ name: '點擊量', data:{{ clicknum_list|safe }} }, { name: '點贊量', data: {{ praise_num_list|safe }} }] }); </script>

路由插件

# 點擊量統計
re_path('article/click/', article.click,name='article/click/'),

後臺方法code

from blog.utils import function # 點擊量
def click(request): recent_seven_days = function.recent_seven_days() list_week_day = recent_seven_days[::-1]  # 進行倒序
    clicknum_list = [] praise_num_list = [] # print(list_week_day)
    for v in list_week_day: click_num_obj = Praise.objects.filter(click_addtime=v,click_sort=1).aggregate(clicknum=Count('click_sort')) praise_num_obj = Praise.objects.filter(click_addtime=v,click_sort=0).aggregate(praise_num=Count('click_sort')) # print(click_num_obj['clicknum'],praise_num_obj['praise_num'])
        clicknum = int(click_num_obj['clicknum']) if (click_num_obj['clicknum'] is not None) else 0 praise_num = int(praise_num_obj['praise_num']) if (praise_num_obj['praise_num'] is not None) else 0 clicknum_list.append(clicknum) praise_num_list.append(praise_num) # print(clicknum_list)

    # data=[{
    # 'name': '點擊量',
    # 'data': clicknum_list
    # }, {
    # 'name': '點贊量',
    # 'data': praise_num_list
    # }]

    # num= [ '20190624', '20190625', '20190626', '20190627', '20190628', '20190629', '20190630']
    return render(request,'article/click.html',locals())

應用目錄下建立untils文件夾,並在其下建立function.py文件,用來獲取最近七天日期htm

# 七天日期
def recent_seven_days():# 經過for 循環獲得天數,若是想獲得兩週的時間,只須要把8改爲15就能夠了。
    import datetime d = datetime.datetime.now()#2019-6-28 9:25:43.843164
    lists = [] for i in range(1,8):#i:1-7
        oneday = datetime.timedelta(days=i) #1 day, 0:00:00 2 days, 0:00:00 ... 7 days, 0:00:00
        day = d - oneday#2019-06-27 11:32:10.186535 2019-06-26 11:32:10.186535 ... 2019-06-21 11:32:10.186535
        date_to = datetime.datetime(day.year, day.month, day.day)#2019-06-27 00:00:00 2019-06-26 00:00:00 ... 2019-06-21 00:00:00
        lists.append(str(date_to)[0:10])#2019-06-27 2019-06-26 ... 2019-06-21
    return lists

頁面效果blog

 

 done。事件

相關文章
相關標籤/搜索