Ticket #18476: 18476-2.diff

File 18476-2.diff, 4.3 KB (added by Claude Paroz, 8 years ago)

More extensive usage of url template tag

  • docs/intro/tutorial03.txt

    diff --git a/docs/intro/tutorial03.txt b/docs/intro/tutorial03.txt
    index fd3a04b..ea36913 100644
    a b under "/content/polls/", or any other path root, and the app will still work. 
    534534All the poll app cares about is its relative path, not its absolute path.
     536Removing hardcoded URLs in templates
     539Remember, when we wrote the link to a poll in our template, the link was
     540partially hardcoded like this:
     542.. code-block:: html+django
     544    <li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li>
     546Now to keep the advantage of decoupled URLs we've just introduced, we can use
     547the :ttag:`url` template tag and replace the hardcoded link:
     549.. code-block:: html+django
     551    <li><a href="{% url 'polls.views.detail' poll.id %}">{{ poll.question }}</a></li>
    536553When you're comfortable with writing views, read :doc:`part 4 of this tutorial
    537554</intro/tutorial04>` to learn about simple form processing and generic views.
  • docs/intro/tutorial04.txt

    diff --git a/docs/intro/tutorial04.txt b/docs/intro/tutorial04.txt
    index 44b9c16..5081853 100644
    a b tutorial, so that the template contains an HTML ``<form>`` element: 
    1919    {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
    21     <form action="/polls/{{ poll.id }}/vote/" method="post">
     21    <form action="{% url 'polls.views.vote' poll.id %}" method="post">
    2222    {% csrf_token %}
    2323    {% for choice in poll.choice_set.all %}
    2424        <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
    A quick rundown: 
    3535  selects one of the radio buttons and submits the form, it'll send the
    3636  POST data ``choice=3``. This is HTML Forms 101.
    38 * We set the form's ``action`` to ``/polls/{{ poll.id }}/vote/``, and we
     38* We set the form's ``action`` to ``'polls.views.vote' poll.id``, and we
    3939  set ``method="post"``. Using ``method="post"`` (as opposed to
    4040  ``method="get"``) is very important, because the act of submitting this
    4141  form will alter data server-side. Whenever you create a form that alters
    Now, create a ``results.html`` template: 
    172172    {% endfor %}
    173173    </ul>
    175     <a href="/polls/{{ poll.id }}/">Vote again?</a>
     175    <a href="{% url 'polls.views.detail' poll.id %}">Vote again?</a>
    177177Now, go to ``/polls/1/`` in your browser and vote in the poll. You should see a
    178178results page that gets updated each time you vote. If you submit the form
    Change it like so:: 
    238238            ListView.as_view(
    239239                queryset=Poll.objects.order_by('-pub_date')[:5],
    240240                context_object_name='latest_poll_list',
    241                 template_name='polls/index.html')),
     241                template_name='polls/index.html'),
     242            name='poll_index'),
    242243        url(r'^(?P<pk>\d+)/$',
    243244            DetailView.as_view(
    244245                model=Poll,
    245                 template_name='polls/detail.html')),
     246                template_name='polls/detail.html'),
     247            name='poll_detail'),
    246248        url(r'^(?P<pk>\d+)/results/$',
    247249            DetailView.as_view(
    248250                model=Poll,
    two views abstract the concepts of "display a list of objects" and 
    265267  ``"pk"``, so we've changed ``poll_id`` to ``pk`` for the generic
    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
    270272  documentation about :ref:`naming URL patterns
    271273  <naming-url-patterns>` for information). We're also using the
    272274  :func:`~django.conf.urls.url` function from
    function anymore -- generic views can be (and are) used multiple times 
    318320    return HttpResponseRedirect(reverse('poll_results', args=(p.id,)))
     322The same rule apply for the :ttag:`url` template tag. For example in the
     323``results.html`` template:
     325.. code-block:: html+django
     327    <a href="{% url 'poll_detail' poll.id %}">Vote again?</a>
    320329Run the server, and use your new polling app based on generic views.
    322331For full details on generic views, see the :doc:`generic views documentation
Back to Top