Author: mtredinnick
Date: 2007-05-27 08:41:10 -0500 (Sun, 27 May 2007)
New Revision: 5368

Modified:
   django/trunk/docs/tutorial04.txt
Log:
Fixed #4221 -- Removed dependence on hard-coded URL in tutorial 4.


Modified: django/trunk/docs/tutorial04.txt
===================================================================
--- django/trunk/docs/tutorial04.txt    2007-05-27 13:20:48 UTC (rev 5367)
+++ django/trunk/docs/tutorial04.txt    2007-05-27 13:41:10 UTC (rev 5368)
@@ -48,6 +48,7 @@
 
     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 @@
             # 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 @@
     * 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 @@
 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
 ======================================


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to