polls app, part 2; assignment error: timezone.now()

using shell, have entered:
from django.utils import timezone
q = Question(question_text=“What’s new ?”, pub_date=timezone.now())

if i type in:

the output is:
datetime.datetime(2023, 2, 11, 12, 53, 20, 995916, tzinfo=datetime.timezone.utc)

why then when i type in:

why does the output have this error ?
name ‘pub_date’ is not defined

It’s because you’re not setting a local variable to that value.

You’re creating an instance of an object of type Question.

That instance is named q.

You’re setting the pub_date attribute of q to the current time.

If you want to see what was set, you would print(q.pub_date)

1 Like

thanks for explaining so much to so many questions on this forum.
i hope you continue to have such patience.
i was stuck for many days on the ‘Playing with the API’ line:


kept getting error:
‘Question’ object has no attribute ‘was_published_recently’

if i am understanding this correctly, ‘was_published_recently’ is defined this way:

def was_published_recently(self):
    return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

i incorrectly thought the problem was in ‘pub_date’
so in an attempt to troubleshoot, i kept trying to print (pub_date)
ugh !
thanks for helping me move forward !

1 Like