Error in testing script

76 views
Skip to first unread message

Gary Roach

unread,
Jul 10, 2016, 4:58:40 PM7/10/16
to django-users
Hi all;

OS Debian Linux KDE desktop
Django 1.9
Python 3.5

Working with tutorial
https://docs.djangoproject.com/en/1.9/intro/tutorial05/
Writing polls/tests.py script

I am consistently getting an error:
NameError: name 'create_question' is not defined.

The first section of tests.py to throw this error is:
def test_index_view_with_two_past_questions(self):
"""
The questions index page may display multiple questions.
"""
create_question(question_text="Past question 1.", days=-30)
create_question(question_text="Past question 2.", days=-5)
response = self.client.get(reverse('polls:index'))
self.assertQuerysetEqual(
response.context['latest_question_list'],
['<Question: Past question 2.>', '<Question: Past question
1.>']
)

The code example is in class QuestionMethodTests(TestCase): but is
flagged 8 times by the editor throughout the code in test.py. The same
error pops at run time.

The code seems to be identical to the code in the tutorial.

All of the errors occur on code lines like:
create_question(question_text=" .......

All help will be appreciated.

Gary R.

Gary Roach

unread,
Jul 10, 2016, 5:26:41 PM7/10/16
to django...@googlegroups.com
I missed something. The create_question call is a def defined earlier in
the class.

def create_question(question_text, days):
"""
Creates a question with the given `question_text` and published the
given number of `days` offset to now (negative for questions
published
in the past, positive for questions that have yet to be published).
"""
time = timezone.now() + datetime.timedelta(days=days)
return Question.objects.create(question_text=question_text,
pub_date=time)

this code throws an error : Method 'create_question - polls.tests'
should have self as first parameter.

Here again, the code seems to be identical to that in the tutorial.
Adding self to the def clears this problem but does not clear the rest
of the errors. The def doesn't seem to be working.

Gary R

Michal Petrucha

unread,
Jul 11, 2016, 2:21:04 AM7/11/16
to django...@googlegroups.com
The code snippet in the tutorial does not put ``create_question``
inside the ``QuestionViewTests`` class, but rather at module level.
That makes a difference.

The way it is written in the tutorial, ``create_question`` is just a
global function that you can call from anywhere as it is.

If you put the ``def`` statement inside the class, that means you are
defining a method in that class, in which case you have to treat it as
such. Unless you decorate it as a static method, or a class method,
you can only call it on an instance of that class, and, just like any
other method in Python, will need to take the class instance as its
first argument, most commonly called ``self``.

Does this help clear it up at least a little bit?

Good luck,

Michal
signature.asc

Gary Roach

unread,
Jul 11, 2016, 3:07:04 PM7/11/16
to django...@googlegroups.com
Thanks Michal

I didn't catch that the def statement was shifted to the left margin.
Works fine now.

Gary R

Reply all
Reply to author
Forward
0 new messages