Django Part 2 Tutorial Name not defined

Hi to all
Hopefully you can help me on my struggle in following the Part 2 Django tutorial. Here’s the code

import datetime
from datetime import timedelta
from django.utils import timezone
from django.db import models
class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __str__(self):
        return self.question_text
    def was_published_recently(self):
        return self.pub_date >= ( - timedelta(days=1))

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default = 0)
    def __str__(self):
        return self.choice_text

after all the code is written and typing in my command line q.was_published_recenlty()
I got the following result:
File “console”, line 1, in module
File “C:\User… \”, line 12, in was_published_recently…
Name Error:name ‘pub_date’ is not defined

Hi, did you create and run migration?

I follow what was written in this tutrial (
and after typing the command line of q.was_published_recently()
and then I got the error message.

What am I lacking?

Go back and rerun the steps from here:

The “makemigrations” step should show something like “no changes detected”, which is ok.

What’s more important is that the sqlmigrate command generates output like what the tutorial shows.

Then run the migrate command after that. Again, it will either show the output as shown or “No migrations to apply”.

If it does work as displayed, exit the shell, re-enter the shell, then retry the commands you were entering in the shell when the error message appeared.

If you’re still having problems, copy the text of the commands and output from the commands above as well as the text from your shell session with the statements you’re entering.

Thnks for the prompt reply sir KenWhitesell,
after re-doing the Part 2 tutorial. I’m lost with this trace back
I’m in the code portion of-

Get the question that was published this year.
>>> from django.utils import timezone
>>> current_year =
>>> Question.objects.get(pub_date__year=current_year)
<Question: What's up?>

and I got Trace back
File "<console>", line 1, in <module>
File "C:\Users\Racho\myproject\lib\site-packages\django\db\models\", line 82, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "C:\Users\Racho\myproject\lib\site-packages\django\db\models\", line 419, in get raise self.model.MultipleObjectsReturned( polls.models.Question.MultipleObjectsReturned: get() returned more than one Question -- it returned 2!


The key is the last line of the traceback -
polls.models.Question.MultipleObjectsReturned: get() returned more than one Question -- it returned 2!

You’re using the get method, so you probably want to review the documentation on get.

Then review what you have done previously, and think about what might have happened to create this situation.

use filter() insted of get()