Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#22912 closed Cleanup/optimization (fixed)

Warning about missing setup_test_environment() call in tutorial 5

Reported by: cfperea@… Owned by: nobody
Component: Documentation Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi, I've been following the official Django tutorial and came across a possible error in the documentation using version 1.6.
In the section titled The Django test client (https://docs.djangoproject.com/en/1.6/intro/tutorial05/#the-django-test-client) in part 5 there is this section of code:

>>> # get a response from '/'
>>> response = client.get('/')
>>> # we should expect a 404 from that address
>>> response.status_code
404
>>> # on the other hand we should expect to find something at '/polls/'
>>> # we'll use 'reverse()' rather than a hardcoded URL
>>> from django.core.urlresolvers import reverse
>>> response = client.get(reverse('polls:index'))
>>> response.status_code
200
>>> response.content
'\n\n\n    <p>No polls are available.</p>\n\n'
>>> # note - you might get unexpected results if your ``TIME_ZONE``
>>> # in ``settings.py`` is not correct. If you need to change it,
>>> # you will also need to restart your shell session
>>> from polls.models import Poll
>>> from django.utils import timezone
>>> # create a Poll and save it
>>> p = Poll(question="Who is your favorite Beatle?", pub_date=timezone.now())
>>> p.save()
>>> # check the response once again
>>> response = client.get('/polls/')
>>> response.content
'\n\n\n    <ul>\n    \n        <li><a href="/polls/1/">Who is your favorite Beatle?</a></li>\n    \n    </ul>\n\n'
>>> response.context['latest_poll_list']
[<Poll: Who is your favorite Beatle?>]

However, when I wrote this test in the console the following error occurred:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: 'NoneType' object is not subscriptable

I changed the line

response.context['latest_poll_list']

to

response.context_data['latest_poll_list']

and it worked as intended.

I'm using Python 2.7 with Django 1.6 in a Windows 7 machine.

Change History (4)

comment:1 by Tim Graham, 10 years ago

Summary: Possible error in documentationWarning about missing setup_test_environment() call in tutorial 5
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

Ah, I figured this out. If you omit setup_test_environment() mentioned earlier, you'll run into this. We should add a warning as it's been reported several times.

comment:2 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: newclosed

In 1e8411259f14f2ef788cc4e7c2a4cdbb537d39b3:

Fixed #22912 -- Warned about a common mistake in tutorial 5.

comment:3 by Tim Graham <timograham@…>, 10 years ago

In 044d419c212478ba42c9f5887ba224414503b9c1:

[1.6.x] Fixed #22912 -- Warned about a common mistake in tutorial 5.

Backport of 1e8411259f from master

comment:4 by Tim Graham <timograham@…>, 10 years ago

In 8e48ca47e956ad2ec7eb19fca6eae3a58c0bf6c8:

[1.7.x] Fixed #22912 -- Warned about a common mistake in tutorial 5.

Backport of 1e8411259f from master

Note: See TracTickets for help on using tickets.
Back to Top