首页 > 解决方案 > /new-watch-hour-graph/ 处的 IndexError

问题描述

def getNewWatchedCountGraph(requests):
    data = Video.getNewWatchedCountGraph(requests)
    data = json.loads(data)
    # print(data)
    x = []
    m = []
    bg = {}
    res = {}
    monthnumbers = []

    currentMonth = datetime.datetime.now().month
    for item in data:
        seconds = int(item['count'])
        x.append(seconds)
        mydate = datetime.datetime.strptime(item['_id'], "%Y-%m")
        monthnumbers.append(mydate.month)
        m.append(mydate.strftime("%B"))

    startMonths = monthnumbers[0] #line 116
    endMonths = currentMonth+1

    data = []
    mon = []

    for months in range(startMonths,endMonths):
        if months not in monthnumbers:
            mon.append(calendar.month_name[months])
            data.append(0)
        else:
            mon.append(calendar.month_name[months])
            monthIndex = monthnumbers.index(months)
            data.append(x[monthIndex])


    res['series_name'] = "Views"
    res['series'] = list(data)
    res['xdata'] = list(mon)

    restrn_response = dumps(res)
    return HttpResponse(restrn_response)

我制作了这个函数来显示视图总数的图表。它在我的本地服务器上运行良好。但是在第 116 行的主服务器中显示列表索引超出范围。我在哪里做错了?

标签: python-3.x

解决方案


发生这种情况是因为monthnumbers是空的。鉴于它在迭代时被填充data,我认为循环甚至没有开始,因为data它是空的。


推荐阅读