Django DateTimeField很幼稚,但使用的是_TZ=Tru

2024-10-05 19:18:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我的Debian服务器出现以下问题。注意:这个问题不会发生在本地Mac env上,相同的代码库。在

if self.ends < timezone.now():Exception Type: TypeError at /dealvote/Exception Value: can't compare offset-naive and offset-aware

我做了以下工作:

在中使用_TZ=True设置.py 时区='UTC'输入设置.py 安装pytz 已安装的mysql时区表mysql_tzinfo_to_sql-(如这里所建议)

代码:

^{pr2}$

末端设置如下:

def setEnd(self, mins, hrs, dys):
    self.ends = timezone.now()
    isFlashDeal = False

    try:
        minutes = int(mins)
        self.ends += timedelta(minutes=int(mins))
        isFlashDeal = True

    except Exception:
        pass

    try:
        self.ends += timedelta(hours=int(hrs))
        isFlashDeal = True
    except Exception:
        pass

    try:
        self.ends += timedelta(days=int(dys))
        isFlashDeal = True
    except Exception:
        pass

    if isFlashDeal == False:
        self.ends = None

    if timezone.is_naive(self.ends):
        self.ends.replace(tzinfo=timezone.utc)

Tags: 代码selftrueifexceptionpassnowtimedelta