diff options
Diffstat (limited to 'docs/tutorial04.txt')
| -rw-r--r-- | docs/tutorial04.txt | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/docs/tutorial04.txt b/docs/tutorial04.txt index 5cc12c445d..bd16fa2924 100644 --- a/docs/tutorial04.txt +++ b/docs/tutorial04.txt @@ -8,8 +8,8 @@ application and will focus on simple form processing and cutting down our code. Write a simple form =================== -Let's update our poll detail template from the last tutorial, so that the -template contains an HTML ``<form>`` element:: +Let's update our poll detail template ("polls/detail.html") from the last +tutorial, so that the template contains an HTML ``<form>`` element:: <h1>{{ poll.question }}</h1> @@ -193,7 +193,7 @@ Change it like so:: 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), - (r'^(?P<object_id>\d+)/results/$', 'django.views.generic.list_detail.object_detail', dict(info_dict, template_name='polls/results.html')), + url(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'), ) @@ -209,6 +209,15 @@ 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 ``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 the documentation about + `naming URL patterns`_ for information). We're also using the `url()`_ + function from ``django.conf.urls.defaults`` here. It's a good habit to + use ``url()`` when you are providing a pattern name like this. + +.. _naming URL patterns: ../url_dispatch/#naming-url-patterns +.. _url(): ../url_dispatch/#url + 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 ``"polls/poll_detail.html"``. Thus, rename your ``polls/detail.html`` template to @@ -255,6 +264,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 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. For full details on generic views, see the `generic views documentation`_. |
