我是 Django 新手,
我正在学习教程,我必须创建两个模型,如下所示:
import datetime
from django.db import models
from django.utils import timezone
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.question
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField()
def __unicode__(self):
return self.choice_text
以下代码来自教程。我应该得到真实的。
# Make sure our custom method worked.
>>> p = Poll.objects.get(pk=1)
>>> p.was_published_recently()
True
但是当我输入(与教程相同的行)时:
>>> p = Poll.objects.get(pk=1)
>>> p.was_published_recently()
我收到以下错误消息:
models.py line 12 in was_published_recently
NameError: global name 'datetime' is not defined..
我导入datetime
了timezone
......我不明白为什么我会收到该错误消息。
任何帮助将不胜感激!:-)