首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Strftime错误Python

Strftime错误Python
EN

Stack Overflow用户
提问于 2018-06-19 21:05:46
回答 1查看 560关注 0票数 0

我调用了一个函数来计算司机的收入,但是,我一直收到这个错误:

代码语言:javascript
复制
"line 396, in driver_get_revenue
    monthly[month.strftime("%m")] = orders.count() * settings.DRIVER_DELIVERY_PRICE
AttributeError: 'int' object has no attribute 'strftime'"

函数是这样的:

代码语言:javascript
复制
def driver_get_revenue(request):

        driver = JWTAuthentication().authenticate(request)[0].driver


        #Returns the difference between date and time.
        from datetime import timedelta

        revenue = {}
        monthly = {}
        yearly = {}


        today = timezone.now()
        month = today.month
        year = today.year


        #Created a range to calculate the current weekday.
        current_weekdays = [today + timedelta(days = i) for i in range(0 - today.weekday(), 7 - today.weekday())]

        for day in current_weekdays:
            orders = Order.objects.filter(
                driver = driver,
                status = Order.DELIVERED,
                created_at__year = day.year,
                created_at__month = day.month,
                created_at__day = day.day
            )
            revenue[day.strftime("%A")] = orders.count() * settings.DRIVER_DELIVERY_PRICE




        for day in range(0, 30):
            orders = Order.objects.filter(
                    driver = driver,
                    status = Order.DELIVERED,
                    created_at__month = month,
                    created_at__day = day
                )

          (Line 396)  monthly[month.strftime("%m")] = orders.count() * settings.DRIVER_DELIVERY_PRICE




        for month in range(0, 12):
            orders = Order.objects.filter(
                    driver = driver,
                    status = Order.DELIVERED,
                    created_at__year = year,
                    created_at__month = month

                )

        yearly[year.strftime("%y")] = orders.count() * settings.DRIVER_DELIVERY_PRICE

        return JsonResponse({"revenue": revenue,
                            "month": monthly,
                            "yearly": yearly})

我不太确定我哪里出错了。我标记了第396行,这样您就可以看到错误所在。任何帮助都将不胜感激。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2018-06-19 21:08:12

执行此操作时:month = today.month,month将变为整数。strftime函数使用datetime对象,而不是整数。

因此,month.strftime("%m")不起作用。

请尝试使用day.strftime("%m"),或者只使用month,这取决于您的需求。

如果你要查找月份的名称,你可以这样做:

代码语言:javascript
复制
today = timezone.now()
month = today.month
month_name = today.strftime("%B") # e.g. December
...

...and在代码中使用month_name变量。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50929237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档