Changeset 678
- Timestamp:
- 09/23/05 17:50:05 (3 years ago)
- Files:
-
- django/trunk/docs/overview.txt (modified) (2 diffs)
- django/trunk/docs/tutorial03.txt (modified) (4 diffs)
- django/trunk/docs/tutorial04.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/docs/overview.txt
r621 r678 196 196 article_detail from above:: 197 197 198 from django.models.news import articles199 200 198 def article_detail(request, year, month, article_id): 201 199 # Use the Django API to find an object matching the URL criteria. 202 try: 203 a = articles.get_object(pub_date__year=year, pub_date__month=month, pk=article_id) 204 except articles.ArticleDoesNotExist: 205 raise Http404 206 t = template_loader.get_template('news/article_detail') 207 c = Context(request, { 208 'article': a, 209 }) 210 content = t.render(c) 211 return HttpResponse(content) 200 a = get_object_or_404(articles, pub_date__year=year, pub_date__month=month, pk=article_id) 201 return render_to_response('news/article_detail', {'article': a}) 212 202 213 203 This example uses Django's template system, which has several key features. … … 261 251 262 252 Here's what the "base" template might look like:: 263 264 253 265 254 <html> django/trunk/docs/tutorial03.txt
r602 r678 193 193 194 194 from django.core import template_loader 195 from django.core. extensions import DjangoContext asContext195 from django.core.template import Context 196 196 from django.models.polls import polls 197 197 from django.utils.httpwrappers import HttpResponse … … 200 200 latest_poll_list = polls.get_list(order_by=['-pub_date'], limit=5) 201 201 t = template_loader.get_template('polls/index') 202 c = Context( request,{202 c = Context({ 203 203 'latest_poll_list': latest_poll_list, 204 204 }) … … 243 243 containing the "What's up" poll from Tutorial 1. 244 244 245 A shortcut: render_to_response() 246 -------------------------------- 247 248 It's a very common idiom to load a template, fill a context and return an 249 ``HttpResponse`` object with the result of the rendered template. Django 250 provides a shortcut. Here's the full ``index()`` view, rewritten:: 251 252 from django.core.extensions import render_to_response 253 from django.models.polls import polls 254 255 def index(request): 256 latest_poll_list = polls.get_list(order_by=['-pub_date'], limit=5) 257 return render_to_response('polls/index', {'latest_poll_list': latest_poll_list}) 258 259 Note that we no longer need to import ``template_loader``, ``Context`` or 260 ``HttpResponse``. 261 262 The ``render_to_response()`` function takes a template name as its first 263 argument and a dictionary as its optional second argument. It returns an 264 ``HttpResponse`` object of the given template rendered with the given context. 265 245 266 Raising 404 246 267 =========== … … 255 276 except polls.PollDoesNotExist: 256 277 raise Http404 257 t = template_loader.get_template('polls/detail') 258 c = Context(request, { 259 'poll': p, 260 }) 261 return HttpResponse(t.render(c)) 278 return render_to_response('polls/detail', {'poll': p}) 262 279 263 280 The new concept here: The view raises the ``django.core.exceptions.Http404`` 264 281 exception if a poll with the requested ID doesn't exist. 282 283 A shortcut: get_object_or_404() 284 ------------------------------- 285 286 It's a very common idiom to use ``get_object()`` and raise ``Http404`` if the 287 object doesn't exist. Django provides a shortcut. Here's the ``detail()`` view, 288 rewritten: 289 290 from django.core.extensions import get_object_or_404 291 def detail(request, poll_id): 292 p = get_object_or_404(polls, pk=poll_id) 293 return render_to_response('polls/detail', {'poll': p}) 294 295 The ``get_object_or_404()`` function takes a Django model module as its first 296 argument and an arbitrary number of keyword arguments, which it passes to the 297 module's ``get_object()`` function. It raises ``Http404`` if the object doesn't 298 exist. 299 300 .. admonition:: Philosophy 301 302 Why do we use a helper function ``get_object_or_404()`` instead of 303 automatically catching the ``*DoesNotExist`` exceptions at a higher level, 304 or having the model API raise ``Http404`` instead of ``*DoesNotExist``? 305 306 Because that would couple the model layer to the view layer. One of the 307 foremost design goals of Django is to maintain loose coupling. 308 309 There's also a ``get_list_or_404()`` function, which works just as 310 ``get_object_or_404()`` -- except using ``get_list()`` instead of 311 ``get_object()``. It raises ``Http404`` if the list is empty. 265 312 266 313 Write a 404 (page not found) view django/trunk/docs/tutorial04.txt
r603 r678 49 49 So let's create a ``vote()`` function in ``myproject/apps/polls/views/polls.py``:: 50 50 51 from django.core import template_loader 52 from django.core.extensions import DjangoContext as Context 51 from django.core.extensions import get_object_or_404, render_to_response 53 52 from django.models.polls import choices, polls 54 from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect 55 from django.core.exceptions import Http404 53 from django.utils.httpwrappers import HttpResponseRedirect 56 54 57 55 def vote(request, poll_id): 58 try: 59 p = polls.get_object(pk=poll_id) 60 except polls.PollDoesNotExist: 61 raise Http404 56 p = get_object_or_404(polls, pk=poll_id) 62 57 try: 63 58 selected_choice = p.get_choice(pk=request.POST['choice']) 64 59 except (KeyError, choices.ChoiceDoesNotExist): 65 60 # Redisplay the poll voting form. 66 t = template_loader.get_template('polls/detail') 67 c = Context(request, { 61 return render_to_response('polls/detail', { 68 62 'poll': p, 69 63 'error_message': "You didn't select a choice.", 70 64 }) 71 return HttpResponse(t.render(c))72 65 else: 73 66 selected_choice.votes += 1 … … 110 103 111 104 def results(request, poll_id): 112 try: 113 p = polls.get_object(pk=poll_id) 114 except polls.PollDoesNotExist: 115 raise Http404 116 t = template_loader.get_template('polls/results') 117 c = Context(request, { 118 'poll': p, 119 }) 120 return HttpResponse(t.render(c)) 105 p = get_object_or_404(polls, pk=poll_id) 106 return render_to_response('polls/results', {'poll': p}) 121 107 122 108 This is almost exactly the same as the ``detail()`` view from `Tutorial 3`_.
