On 07/10/2016 11:19 PM, Michal Petrucha wrote:
On Sun, Jul 10, 2016 at 02:25:40PM -0700, Gary Roach wrote:
On 07/10/2016 01:57 PM, Gary Roach wrote:
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.

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
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

Thanks Michal

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

Gary R

--
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/589a8274-8f98-2d86-c57f-ef30715559b6%40verizon.net.
For more options, visit https://groups.google.com/d/optout.

Reply via email to