#22912 closed Cleanup/optimization (fixed)
Warning about missing setup_test_environment() call in tutorial 5
Reported by: | 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 , 10 years ago
Summary: | Possible error in documentation → Warning about missing setup_test_environment() call in tutorial 5 |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Bug → Cleanup/optimization |
comment:2 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
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.