Ticket #6550: tutorial4_6550.diff

File tutorial4_6550.diff, 2.6 KB (added by Vegpuff, 17 years ago)

Making changes in tutorial3 so as be similar in tutorial4.

  • tutorial03.txt

     
    2525
    2626In our poll application, we'll have the following four views:
    2727
    28     * Poll "archive" page -- displays the latest few polls.
     28    * Poll "archive" page -- displays the list of polls.
    2929    * Poll "detail" page -- displays a poll question, with no results but
    3030      with a form to vote.
    3131    * Poll "results" page -- displays results for a particular poll.
     
    183183
    184184Because it's convenient, let's use Django's own database API, which we covered
    185185in Tutorial 1. Here's one stab at the ``index()`` view, which displays the
    186 latest 5 poll questions in the system, separated by commas, according to
     186list of poll questions in the system, separated by commas, according to
    187187publication date::
    188188
    189189    from mysite.polls.models import Poll
    190190    from django.http import HttpResponse
    191191
    192192    def index(request):
    193         latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
    194         output = ', '.join([p.question for p in latest_poll_list])
     193        poll_list = Poll.objects.all().order_by('-pub_date')
     194        output = ', '.join([p.question for p in poll_list])
    195195        return HttpResponse(output)
    196196
    197197There's a problem here, though: The page's design is hard-coded in the view. If
     
    203203    from django.http import HttpResponse
    204204
    205205    def index(request):
    206         latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
     206        poll_list = Poll.objects.all().order_by('-pub_date')
    207207        t = loader.get_template('polls/index.html')
    208208        c = Context({
    209             'latest_poll_list': latest_poll_list,
     209            'poll_list': poll_list,
    210210        })
    211211        return HttpResponse(t.render(c))
    212212
     
    234234
    235235Put the following code in that template::
    236236
    237     {% if latest_poll_list %}
     237    {% if poll_list %}
    238238        <ul>
    239         {% for poll in latest_poll_list %}
     239        {% for poll in poll_list %}
    240240            <li>{{ poll.question }}</li>
    241241        {% endfor %}
    242242        </ul>
     
    258258    from mysite.polls.models import Poll
    259259
    260260    def index(request):
    261         latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
    262         return render_to_response('polls/index.html', {'latest_poll_list': latest_poll_list})
     261        poll_list = Poll.objects.all().order_by('-pub_date')
     262        return render_to_response('polls/index.html', {'poll_list': poll_list})
    263263
    264264Note that once we've done this in all these views, we no longer need to import ``loader``, ``Context`` and ``HttpResponse``.
    265265
Back to Top