mirror of https://github.com/django/django.git
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
This commit is contained in:
parent
67abbb6ea0
commit
5bb8c84882
|
@ -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
|
||||
======================================
|
||||
|
|
Loading…
Reference in New Issue