Wednesday, March 26, 2014

Referring to the page --

I've looked at this several times, and I was hoping to get the proverbial set of extra eyes.

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 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 >= - 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
To post to this group, send email to
Visit this group at
To view this discussion on the web visit
For more options, visit

No comments:

Post a Comment