Ticket #1464: Tutorial4.diff
File Tutorial4.diff, 2.4 KB (added by , 19 years ago) |
---|
-
H:/Programming/Data/Django-Magic-Removal/docs/tutorial04.txt
49 49 So let's create a ``vote()`` function in ``myproject/polls/views.py``:: 50 50 51 51 from django.shortcuts import get_object_or_404, render_to_response 52 from django.models.polls import choices, polls52 from myproject.polls.models import Choice, Poll 53 53 from django.http import HttpResponseRedirect 54 54 55 55 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) 57 57 try: 58 selected_choice = p. get_choice(pk=request.POST['choice'])58 selected_choice = p.choice_set.filter(pk=request.POST['choice']) 59 59 except (KeyError, choices.ChoiceDoesNotExist): 60 60 # Redisplay the poll voting form. 61 61 return render_to_response('polls/detail', { … … 102 102 page for the poll. Let's write that view:: 103 103 104 104 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) 106 106 return render_to_response('polls/results', {'poll': p}) 107 107 108 108 This is almost exactly the same as the ``detail()`` view from `Tutorial 3`_. … … 168 168 Change it like so:: 169 169 170 170 from django.conf.urls.defaults import * 171 171 from myproject.polls.models import Poll 172 172 173 info_dict = { 173 'app_label': 'polls', 174 'module_name': 'polls', 174 'queryset': Poll.objects.all(), 175 175 } 176 176 177 177 urlpatterns = patterns('', … … 185 185 Respectively, those two views abstract the concepts of "display a list of 186 186 objects" and "display a detail page for a particular type of object." 187 187 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. 192 190 193 191 * The ``object_detail`` generic view expects that the ID value captured 194 192 from the URL is called ``"object_id"``, so we've changed ``poll_id`` to