Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #18476 - Added use of {% url %} tag to tutorial.

Thanks Claude Paroz for the patch.
  • Loading branch information...
commit 690ed5794672495e4cffca9a4a701008d254a4e7 1 parent 07d70e9
Tim Graham authored July 29, 2012
17  docs/intro/tutorial03.txt
@@ -533,5 +533,22 @@ under "/content/polls/", or any other path root, and the app will still work.
533 533
 
534 534
 All the poll app cares about is its relative path, not its absolute path.
535 535
 
  536
+Removing hardcoded URLs in templates
  537
+------------------------------------
  538
+
  539
+Remember, when we wrote the link to a poll in our template, the link was
  540
+partially hardcoded like this:
  541
+
  542
+.. code-block:: html+django
  543
+
  544
+    <li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li>
  545
+
  546
+To use the decoupled URLs we've just introduced, replace the hardcoded link
  547
+with the :ttag:`url` template tag:
  548
+
  549
+.. code-block:: html+django
  550
+
  551
+    <li><a href="{% url 'polls.views.detail' poll.id %}">{{ poll.question }}</a></li>
  552
+
536 553
 When you're comfortable with writing views, read :doc:`part 4 of this tutorial
537 554
 </intro/tutorial04>` to learn about simple form processing and generic views.
23  docs/intro/tutorial04.txt
@@ -18,7 +18,7 @@ tutorial, so that the template contains an HTML ``<form>`` element:
18 18
 
19 19
     {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
20 20
 
21  
-    <form action="/polls/{{ poll.id }}/vote/" method="post">
  21
+    <form action="{% url 'polls.views.vote' poll.id %}" method="post">
22 22
     {% csrf_token %}
23 23
     {% for choice in poll.choice_set.all %}
24 24
         <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
@@ -35,7 +35,7 @@ A quick rundown:
35 35
   selects one of the radio buttons and submits the form, it'll send the
36 36
   POST data ``choice=3``. This is HTML Forms 101.
37 37
 
38  
-* We set the form's ``action`` to ``/polls/{{ poll.id }}/vote/``, and we
  38
+* We set the form's ``action`` to ``{% url 'polls.views.vote' poll.id %}``, and we
39 39
   set ``method="post"``. Using ``method="post"`` (as opposed to
40 40
   ``method="get"``) is very important, because the act of submitting this
41 41
   form will alter data server-side. Whenever you create a form that alters
@@ -172,7 +172,7 @@ Now, create a ``results.html`` template:
172 172
     {% endfor %}
173 173
     </ul>
174 174
 
175  
-    <a href="/polls/{{ poll.id }}/">Vote again?</a>
  175
+    <a href="{% url 'polls.views.detail' poll.id %}">Vote again?</a>
176 176
 
177 177
 Now, go to ``/polls/1/`` in your browser and vote in the poll. You should see a
178 178
 results page that gets updated each time you vote. If you submit the form
@@ -238,11 +238,13 @@ Change it like so::
238 238
             ListView.as_view(
239 239
                 queryset=Poll.objects.order_by('-pub_date')[:5],
240 240
                 context_object_name='latest_poll_list',
241  
-                template_name='polls/index.html')),
  241
+                template_name='polls/index.html'),
  242
+            name='poll_index'),
242 243
         url(r'^(?P<pk>\d+)/$',
243 244
             DetailView.as_view(
244 245
                 model=Poll,
245  
-                template_name='polls/detail.html')),
  246
+                template_name='polls/detail.html'),
  247
+            name='poll_detail'),
246 248
         url(r'^(?P<pk>\d+)/results/$',
247 249
             DetailView.as_view(
248 250
                 model=Poll,
@@ -265,8 +267,8 @@ two views abstract the concepts of "display a list of objects" and
265 267
   ``"pk"``, so we've changed ``poll_id`` to ``pk`` for the generic
266 268
   views.
267 269
 
268  
-* We've added a name, ``poll_results``, to the results view so
269  
-  that we have a way to refer to its URL later on (see the
  270
+* We've added the ``name`` argument to the views (e.g. ``name='poll_results'``)
  271
+  so that we have a way to refer to their URL later on (see the
270 272
   documentation about :ref:`naming URL patterns
271 273
   <naming-url-patterns>` for information). We're also using the
272 274
   :func:`~django.conf.urls.url` function from
@@ -317,6 +319,13 @@ function anymore -- generic views can be (and are) used multiple times
317 319
 
318 320
     return HttpResponseRedirect(reverse('poll_results', args=(p.id,)))
319 321
 
  322
+The same rule apply for the :ttag:`url` template tag. For example in the
  323
+``results.html`` template:
  324
+
  325
+.. code-block:: html+django
  326
+
  327
+    <a href="{% url 'poll_detail' poll.id %}">Vote again?</a>
  328
+
320 329
 Run the server, and use your new polling app based on generic views.
321 330
 
322 331
 For full details on generic views, see the :doc:`generic views documentation

0 notes on commit 690ed57

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