https://docs.djangoproject.com/en/1.6/intro/tutorial01/
I made it almost to the bottom of the page, but while I'm in the Python shell, I type the following:
>>> p = Poll.objects.get(pk=1)
>>> p.was_published_recently()
Here is the contents of the models.py file, which contains the Poll object.
import datetime
from django.utils import timezone
from django.db import models
# Create your models here
from django.db import models
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(default=0)
def __unicode__(self):
return self.choice_text
When I type >>> p.was_published_recently(), I expected to see "True" because the published date satisfies the definition, but instead i'm presented with:
AttributeError: 'Poll' object has no attribute 'was_published_recently'
I'm new at this, so please forgive me if this seems like a silly question to ask. Looking at the Poll class and the definition that I added, I'm not seeing why the AttributeError would be thrown.
I quadruple-checked for typos, too. If anyone has an idea, please let me know.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c2e85d87-cd00-405d-8057-995411cce6d8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment