Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #4221 -- Removed dependence on hard-coded URL in tutorial 4.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5368 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 5bb8c8488285cdaa0563ca5e9930bdf52f004f55 1 parent 67abbb6
Malcolm Tredinnick authored May 27, 2007

Showing 1 changed file with 20 additions and 3 deletions. Show diff stats Hide diff stats

  1. 23  docs/tutorial04.txt
23  docs/tutorial04.txt
@@ -48,6 +48,7 @@ So let's create a ``vote()`` function in ``mysite/polls/views.py``::
48 48
 
49 49
     from django.shortcuts import get_object_or_404, render_to_response
50 50
     from django.http import HttpResponseRedirect
  51
+    from django.core.urlresolvers import reverse
51 52
     from mysite.polls.models import Choice, Poll
52 53
     # ...
53 54
     def vote(request, poll_id):
@@ -66,7 +67,7 @@ So let's create a ``vote()`` function in ``mysite/polls/views.py``::
66 67
             # Always return an HttpResponseRedirect after successfully dealing
67 68
             # with POST data. This prevents data from being posted twice if a
68 69
             # user hits the Back button.
69  
-            return HttpResponseRedirect('/polls/%s/results/' % p.id)
  70
+            return HttpResponseRedirect(reverse('results', args=(p.id,)))
70 71
 
71 72
 This code includes a few things we haven't covered yet in this tutorial:
72 73
 
@@ -86,13 +87,28 @@ This code includes a few things we haven't covered yet in this tutorial:
86 87
     * After incrementing the choice count, the code returns an
87 88
       ``HttpResponseRedirect`` rather than a normal ``HttpResponse``.
88 89
       ``HttpResponseRedirect`` takes a single argument: the URL to which the
89  
-      user will be redirected. You should leave off the "http://" and domain
90  
-      name if you can. That helps your app become portable across domains.
  90
+      user will be redirected (see the following point for how we construct
  91
+      the URL in this case).
91 92
 
92 93
       As the Python comment above points out, you should always return an
93 94
       ``HttpResponseRedirect`` after successfully dealing with POST data. This
94 95
       tip isn't specific to Django; it's just good Web development practice.
95 96
 
  97
+    * We are using the ``reverse()`` function in the ``HttpResponseRedirect``
  98
+      constructor in this example. This function helps avoid having to
  99
+      hardcode a URL in the view function. It is given the name of the view
  100
+      that we want to pass control to and the variable portion of the URL
  101
+      pattern that points to that view. In this case, using the URLConf we set
  102
+      up in Tutorial 3, this ``reverse()`` call will return a string like ::
  103
+
  104
+        '/polls/3/results/'
  105
+
  106
+      ... where the ``3`` is the value of ``p.id``. This redirected URL will
  107
+      then call the ``'results'`` view to display the final page.
  108
+
  109
+      For more information about ``reverse()``, see the `URL dispatcher`_
  110
+      documentation.
  111
+
96 112
 As mentioned in Tutorial 3, ``request`` is a ``HTTPRequest`` object. For more
97 113
 on ``HTTPRequest`` objects, see the `request and response documentation`_.
98 114
 
@@ -121,6 +137,7 @@ results page that gets updated each time you vote. If you submit the form
121 137
 without having chosen a choice, you should see the error message.
122 138
 
123 139
 .. _request and response documentation: ../request_response/
  140
+.. _URL dispatcher: ../url_dispatch#reverse
124 141
 
125 142
 Use generic views: Less code is better
126 143
 ======================================

0 notes on commit 5bb8c84

Please sign in to comment.
Something went wrong with that request. Please try again.