summaryrefslogtreecommitdiff
path: root/docs/tutorial04.txt
diff options
context:
space:
mode:
Diffstat (limited to 'docs/tutorial04.txt')
-rw-r--r--docs/tutorial04.txt49
1 files changed, 43 insertions, 6 deletions
diff --git a/docs/tutorial04.txt b/docs/tutorial04.txt
index 49ed649cff..5cc12c445d 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('mysite.polls.views.results', args=(p.id,)))
This code includes a few things we haven't covered yet in this tutorial:
@@ -86,13 +87,29 @@ 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. Note that
+ you need to use the full name of the view here (including the prefix).
+
+ 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`_.
@@ -120,7 +137,8 @@ Now, go to ``/polls/1/`` in your browser and vote in the poll. You should see a
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: http://www.djangoproject.com/documentation/request_response/
+.. _request and response documentation: ../request_response/
+.. _URL dispatcher: ../url_dispatch#reverse
Use generic views: Less code is better
======================================
@@ -206,6 +224,21 @@ for the polls app, we manually specify a template name for the results view:
``template_name='polls/results.html'``. Otherwise, both views would use the same
template. Note that we use ``dict()`` to return an altered dictionary in place.
+.. note:: ``all()`` is lazy
+
+ It might look a little frightening to see ``Poll.objects.all()`` being used
+ in a detail view which only needs one ``Poll`` object, but don't worry;
+ ``Poll.objects.all()`` is actually a special object called a ``QuerySet``,
+ which is "lazy" and doesn't hit your database until it absolutely has to. By
+ the time the database query happens, the ``object_detail`` generic view will
+ have narrowed its scope down to a single object, so the eventual query will
+ only select one row from the database.
+
+ If you'd like to know more about how that works, The Django database API
+ documentation `explains the lazy nature of QuerySet objects`_.
+
+.. _explains the lazy nature of QuerySet objects: ../db-api/#querysets-are-lazy
+
In previous parts of the tutorial, the templates have been provided with a context
that contains the ``poll`` and ``latest_poll_list`` context variables. However,
the generic views provide the variables ``object`` and ``object_list`` as context.
@@ -226,7 +259,7 @@ Run the server, and use your new polling app based on generic views.
For full details on generic views, see the `generic views documentation`_.
-.. _generic views documentation: http://www.djangoproject.com/documentation/generic_views/
+.. _generic views documentation: ../generic_views/
Coming soon
===========
@@ -241,4 +274,8 @@ installments:
* Advanced admin features: Permissions
* Advanced admin features: Custom JavaScript
-.. _Tutorial 3: http://www.djangoproject.com/documentation/tutorial3/
+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/