Ticket #6550: tutorial4_6550.diff
File tutorial4_6550.diff, 2.6 KB (added by , 17 years ago) |
---|
-
tutorial03.txt
25 25 26 26 In our poll application, we'll have the following four views: 27 27 28 * Poll "archive" page -- displays the l atest fewpolls.28 * Poll "archive" page -- displays the list of polls. 29 29 * Poll "detail" page -- displays a poll question, with no results but 30 30 with a form to vote. 31 31 * Poll "results" page -- displays results for a particular poll. … … 183 183 184 184 Because it's convenient, let's use Django's own database API, which we covered 185 185 in Tutorial 1. Here's one stab at the ``index()`` view, which displays the 186 l atest 5poll questions in the system, separated by commas, according to186 list of poll questions in the system, separated by commas, according to 187 187 publication date:: 188 188 189 189 from mysite.polls.models import Poll 190 190 from django.http import HttpResponse 191 191 192 192 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]) 195 195 return HttpResponse(output) 196 196 197 197 There's a problem here, though: The page's design is hard-coded in the view. If … … 203 203 from django.http import HttpResponse 204 204 205 205 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') 207 207 t = loader.get_template('polls/index.html') 208 208 c = Context({ 209 ' latest_poll_list': latest_poll_list,209 'poll_list': poll_list, 210 210 }) 211 211 return HttpResponse(t.render(c)) 212 212 … … 234 234 235 235 Put the following code in that template:: 236 236 237 {% if latest_poll_list %}237 {% if poll_list %} 238 238 <ul> 239 {% for poll in latest_poll_list %}239 {% for poll in poll_list %} 240 240 <li>{{ poll.question }}</li> 241 241 {% endfor %} 242 242 </ul> … … 258 258 from mysite.polls.models import Poll 259 259 260 260 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}) 263 263 264 264 Note that once we've done this in all these views, we no longer need to import ``loader``, ``Context`` and ``HttpResponse``. 265 265