Opened 17 years ago
Closed 17 years ago
#4730 closed (duplicate)
Django Tutorial Part 4 - Suggestion
Reported by: | Owned by: | Jacob | |
---|---|---|---|
Component: | Documentation | Version: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
The last paragraph in Django Tutorial Part 4 misses a point about generic views. I had problems getting the /polls/ app to work using generic views. The generic views must be made unique in the URLconf (urls.py) file.
The mysite.polls.views.vote "view" ends with a HttpResponseRedirect, which has to send users to a generic view (for poll results). Since both the "details" and "results" pages have the same python function reference name (generic view), urls.py couldn't tell them apart. I found two solutions to this issue.
1) Reorder urlpatterns in urls.py so that the "results" line is before the "details" one. Then, they will be unique on regular expression, and "results" will match first.
[BETTER]
2) Add the 4th "view-name" parameter to urlpatterns tuples to make the "details" and "results" views unique. I chose to enter this:
urlpatterns = patterns(,
(r'$', 'django.views.generic.list_detail.object_list', info_dict),
(r'(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict, 'poll-ballot'),
(r'(?P<object_id>\d+)/results/$', 'django.views.generic.list_detail.object_detail', dict(info_dict, template_name='polls/results.html'), 'poll-results'),
(r'(?P<poll_id>\d+)/vote/$', 'mysite.polls.views.vote'),
)
Please, change the tutorial near LAST PARAGRAPH to explain all this. This is not an obvious point!
LAST PARAGRAPH (Of Django Tutorial Part 4):
The vote() view is still required. However, it must be modified to match the new templates and context variables. Change the template call from polls/detail.html to polls/poll_detail.html, and pass object in the context instead of poll.
Thanks for the report. Dupe of #4615 however.