无法比较天真和知道datetime.now()< = challenge.datetime_end [英] Can't compare naive and aware datetime.now() <= challenge.datetime_end
本文介绍了无法比较天真和知道datetime.now()< = challenge.datetime_end的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用比较运算符比较当前日期和时间与模型中指定的日期和时间:
I am trying to compare the current date and time with dates and times specified in models using comparison operators:
if challenge.datetime_start <= datetime.now() <= challenge.datetime_end:
脚本错误与:
TypeError: can't compare offset-naive and offset-aware datetimes
这些模型如下所示:
class Fundraising_Challenge(models.Model):
name = models.CharField(max_length=100)
datetime_start = models.DateTimeField()
datetime_end = models.DateTimeField()
我也有django使用区域设置的日期和时间。
I also have django using locale date and times.
't能够找到是django用于DateTimeField()的格式。是天真还是意识?如何获得datetime.now()来识别locale datetime?
What I haven't been able to find is the format django uses for DateTimeField(). Is it naive or aware? And how do I get datetime.now() to recognize locale datetime?
推荐答案
datetime.datetime。
datetime.datetime.now
is not timezone aware.
Django提供了一个辅助程序,需要 pytz
Django comes with a helper for this, which requires pytz
from django.utils import timezone
now = timezone.now()
您应该能够比较现在
到 .datetime_start
这篇关于无法比较天真和知道datetime.now()< = challenge.datetime_end的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文