summaryrefslogtreecommitdiff
path: root/docs/tutorial04.txt
diff options
context:
space:
mode:
Diffstat (limited to 'docs/tutorial04.txt')
-rw-r--r--docs/tutorial04.txt27
1 files changed, 24 insertions, 3 deletions
diff --git a/docs/tutorial04.txt b/docs/tutorial04.txt
index 6fee842f8b..8aa9379c91 100644
--- a/docs/tutorial04.txt
+++ b/docs/tutorial04.txt
@@ -48,6 +48,7 @@ So let's create a ``vote()`` function in ``mysite/polls/views.py``::
from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponseRedirect
+ from django.core.urlresolvers import reverse
from mysite.polls.models import Choice, Poll
# ...
def vote(request, poll_id):
@@ -66,7 +67,7 @@ So let's create a ``vote()`` function in ``mysite/polls/views.py``::
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
- return HttpResponseRedirect('/polls/%s/results/' % p.id)
+ return HttpResponseRedirect(reverse('results', args=(p.id,)))
This code includes a few things we haven't covered yet in this tutorial:
@@ -86,13 +87,28 @@ This code includes a few things we haven't covered yet in this tutorial:
* After incrementing the choice count, the code returns an
``HttpResponseRedirect`` rather than a normal ``HttpResponse``.
``HttpResponseRedirect`` takes a single argument: the URL to which the
- user will be redirected. You should leave off the "http://" and domain
- name if you can. That helps your app become portable across domains.
+ user will be redirected (see the following point for how we construct
+ the URL in this case).
As the Python comment above points out, you should always return an
``HttpResponseRedirect`` after successfully dealing with POST data. This
tip isn't specific to Django; it's just good Web development practice.
+ * We are using the ``reverse()`` function in the ``HttpResponseRedirect``
+ constructor in this example. This function helps avoid having to
+ hardcode a URL in the view function. It is given the name of the view
+ that we want to pass control to and the variable portion of the URL
+ pattern that points to that view. In this case, using the URLConf we set
+ up in Tutorial 3, this ``reverse()`` call will return a string like ::
+
+ '/polls/3/results/'
+
+ ... where the ``3`` is the value of ``p.id``. This redirected URL will
+ then call the ``'results'`` view to display the final page.
+
+ For more information about ``reverse()``, see the `URL dispatcher`_
+ documentation.
+
As mentioned in Tutorial 3, ``request`` is a ``HTTPRequest`` object. For more
on ``HTTPRequest`` objects, see the `request and response documentation`_.
@@ -121,6 +137,7 @@ results page that gets updated each time you vote. If you submit the form
without having chosen a choice, you should see the error message.
.. _request and response documentation: ../request_response/
+.. _URL dispatcher: ../url_dispatch#reverse
Use generic views: Less code is better
======================================
@@ -256,4 +273,8 @@ installments:
* Advanced admin features: Permissions
* Advanced admin features: Custom JavaScript
+In the meantime, you can read through the rest of the `Django documentation`_
+and start writing your own applications.
+
.. _Tutorial 3: ../tutorial03/
+.. _Django documentation: http://www.djangoproject.com/documentation/