我正在学习Jython上的Django,我在执行Django Tutorial 01时遇到了错误。
我的系统
这是我的models.py
的代码来自django.utils导入时区 导入日期时间 来自django.db导入模型
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.question_text
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
class Choice(models.Model):
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __unicode__(self): # __unicode__ on Python 2
return self.choice_text
将对象插入数据库后,我尝试以下命令。
>>> q = Question.objects.get(pk=1)
>>> q.was_published_recently()
然后我收到以下错误消息:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\michmar3\workspace\pollsite\polls\models.py", line 14, in was_p
ublished_recently
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
File "C:\jython2.7b2\Lib\datetime.py", line 1727, in __ge__
return self.__cmp(other) >= 0
File "C:\jython2.7b2\Lib\datetime.py", line 1765, in _datetime__cmp
raise TypeError("cannot compare naive and aware datetimes")
TypeError: cannot compare naive and aware datetimes
答案 0 :(得分:0)
尝试使用datetime.utcnow()
代替timezone.now()