Skip to content

Commit

Permalink
Fixed #4615: corrected reverse URL resolution examples in tutorial 4.…
Browse files Browse the repository at this point in the history
… Thanks for the patch, simeonf.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5649 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jacobian committed Jul 12, 2007
1 parent 12621d1 commit 74fe707
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion docs/tutorial04.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ Change it like so::
urlpatterns = patterns('', urlpatterns = patterns('',
(r'^$', 'django.views.generic.list_detail.object_list', info_dict), (r'^$', 'django.views.generic.list_detail.object_list', info_dict),
(r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict), (r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict),
(r'^(?P<object_id>\d+)/results/$', 'django.views.generic.list_detail.object_detail', dict(info_dict, template_name='polls/results.html')), (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'), (r'^(?P<poll_id>\d+)/vote/$', 'mysite.polls.views.vote'),
) )


Expand All @@ -209,6 +209,12 @@ objects" and "display a detail page for a particular type of object."
from the URL to be called ``"object_id"``, so we've changed ``poll_id`` to from the URL to be called ``"object_id"``, so we've changed ``poll_id`` to
``object_id`` for the generic views. ``object_id`` for the generic views.


* We've added a name, ``poll_results``, to the results view so that we have
a way to refer to its url later on (see `naming URL patterns`_ for more on
named patterns).

.. _naming URL patterns: http://www.djangoproject.com/documentation/url_dispatch/#naming-url-patterns

By default, the ``object_detail`` generic view uses a template called By default, the ``object_detail`` generic view uses a template called
``<app name>/<model name>_detail.html``. In our case, it'll use the template ``<app name>/<model name>_detail.html``. In our case, it'll use the template
``"polls/poll_detail.html"``. Thus, rename your ``polls/detail.html`` template to ``"polls/poll_detail.html"``. Thus, rename your ``polls/detail.html`` template to
Expand Down Expand Up @@ -255,6 +261,15 @@ the new templates and context variables. Change the template call from
``polls/detail.html`` to ``polls/poll_detail.html``, and pass ``object`` in the ``polls/detail.html`` to ``polls/poll_detail.html``, and pass ``object`` in the
context instead of ``poll``. context instead of ``poll``.


The last thing to do is fix the url handling to account for the use of generic
views. In the vote view above we used the ``reverse()`` function to avoid
hard-coding our URLs. Now that we've switched to a generic view, we'll need to
change the ``reverse()`` call to point back to our new generic view. We can't
simply use the view function anymore -- generic views can be (and are) used
multiple times -- but we can use the name we've given::

return HttpResponseRedirect(reverse('poll_results', args=(p.id,)))

Run the server, and use your new polling app based on generic views. Run the server, and use your new polling app based on generic views.


For full details on generic views, see the `generic views documentation`_. For full details on generic views, see the `generic views documentation`_.
Expand Down

0 comments on commit 74fe707

Please sign in to comment.