相关疑难解决方法(0)

无法比较天真和意识到datetime.now()<= challenge.datetime_end

我试图使用比较运算符将当前日期和时间与模型中指定的日期和时间进行比较:

if challenge.datetime_start <= datetime.now() <= challenge.datetime_end:
Run Code Online (Sandbox Code Playgroud)

脚本错误出现:

TypeError: can't compare offset-naive and offset-aware datetimes
Run Code Online (Sandbox Code Playgroud)

模型看起来像这样:

class Fundraising_Challenge(models.Model):
    name = models.CharField(max_length=100)
    datetime_start = models.DateTimeField()
    datetime_end = models.DateTimeField()
Run Code Online (Sandbox Code Playgroud)

我也有使用语言环境日期和时间的django.

我无法找到的是django用于DateTimeField()的格式.是天真还是意识到?如何让datetime.now()识别区域设置日期时间?

python django comparison datetime

119
推荐指数
6
解决办法
12万
查看次数

标签 统计

comparison ×1

datetime ×1

django ×1

python ×1