Ticket #1464: Tutorial4.diff

File Tutorial4.diff, 2.4 KB (added by ChaosKCW, 18 years ago)

Tutorial4 Changes for Magic Removal

  • H:/Programming/Data/Django-Magic-Removal/docs/tutorial04.txt

     
    4949So let's create a ``vote()`` function in ``myproject/polls/views.py``::
    5050
    5151    from django.shortcuts import get_object_or_404, render_to_response
    52     from django.models.polls import choices, polls
     52    from myproject.polls.models import Choice, Poll
    5353    from django.http import HttpResponseRedirect
    5454
    5555    def vote(request, poll_id):
    56         p = get_object_or_404(polls, pk=poll_id)
     56        p = get_object_or_404(Poll, pk=poll_id)
    5757        try:
    58             selected_choice = p.get_choice(pk=request.POST['choice'])
     58            selected_choice = p.choice_set.filter(pk=request.POST['choice'])
    5959        except (KeyError, choices.ChoiceDoesNotExist):
    6060            # Redisplay the poll voting form.
    6161            return render_to_response('polls/detail', {
     
    102102page for the poll. Let's write that view::
    103103
    104104    def results(request, poll_id):
    105         p = get_object_or_404(polls, pk=poll_id)
     105        p = get_object_or_404(Poll, pk=poll_id)
    106106        return render_to_response('polls/results', {'poll': p})
    107107
    108108This is almost exactly the same as the ``detail()`` view from `Tutorial 3`_.
     
    168168Change it like so::
    169169
    170170    from django.conf.urls.defaults import *
    171 
     171    from myproject.polls.models import Poll
     172   
    172173    info_dict = {
    173         'app_label': 'polls',
    174         'module_name': 'polls',
     174        'queryset': Poll.objects.all(),
    175175    }
    176176
    177177    urlpatterns = patterns('',
     
    185185Respectively, those two views abstract the concepts of "display a list of
    186186objects" and "display a detail page for a particular type of object."
    187187
    188     * Each generic view needs to know which ``app_label`` and ``module_name``
    189       it's acting on. Thus, we've defined ``info_dict``, a dictionary that's
    190       passed to each of the generic views via the third parameter to the URL
    191       tuples.
     188    * Each generic view needs to know which model its acting on. This
     189      is done using a QuerySet.
    192190
    193191    * The ``object_detail`` generic view expects that the ID value captured
    194192      from the URL is called ``"object_id"``, so we've changed ``poll_id`` to
Back to Top