summaryrefslogtreecommitdiff
path: root/docs/tutorial03.txt
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2006-04-08 08:39:34 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2006-04-08 08:39:34 +0000
commitf576187df6c110505e9fc1202279c9146a3fc25f (patch)
treedc8c3b2fdf8bd1c4406b49691f7f73bebbd86abf /docs/tutorial03.txt
parentd1083f15fbdf073d768677f683a1e3b5136e3e41 (diff)
magic-removal: Refs #1464 -- Cleaned up some stray tabs that leaked into r2632
git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2633 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'docs/tutorial03.txt')
-rw-r--r--docs/tutorial03.txt52
1 files changed, 26 insertions, 26 deletions
diff --git a/docs/tutorial03.txt b/docs/tutorial03.txt
index b26614a5ee..5855a7d6b2 100644
--- a/docs/tutorial03.txt
+++ b/docs/tutorial03.txt
@@ -187,29 +187,29 @@ in Tutorial 1. Here's one stab at the ``index()`` view, which displays the
latest 5 poll questions in the system, separated by commas, according to
publication date::
- from myproject.polls.models import Poll
- from django.http import HttpResponse
+ from myproject.polls.models import Poll
+ from django.http import HttpResponse
- def index(request):
- latest_poll_list = Poll.objects.all().order_by('-pub_date')
- output = ', '.join([p.question for p in latest_poll_list])
- return HttpResponse(output)
+ def index(request):
+ latest_poll_list = Poll.objects.all().order_by('-pub_date')
+ output = ', '.join([p.question for p in latest_poll_list])
+ return HttpResponse(output)
There's a problem here, though: The page's design is hard-coded in the view. If
you want to change the way the page looks, you'll have to edit this Python code.
So let's use Django's template system to separate the design from Python::
- from django.template import Context, loader
- from myproject.polls.models import Poll
- from django.http import HttpResponse
+ from django.template import Context, loader
+ from myproject.polls.models import Poll
+ from django.http import HttpResponse
- def index(request):
- latest_poll_list = Poll.objects.all().order_by('-pub_date')
- t = loader.get_template('polls/index')
- c = Context({
- 'latest_poll_list': latest_poll_list,
- })
- return HttpResponse(t.render(c))
+ def index(request):
+ latest_poll_list = Poll.objects.all().order_by('-pub_date')
+ t = loader.get_template('polls/index')
+ c = Context({
+ 'latest_poll_list': latest_poll_list,
+ })
+ return HttpResponse(t.render(c))
That code loads the template called "polls/index" and passes it a context. The
context is a dictionary mapping template variable names to Python objects.
@@ -256,11 +256,11 @@ It's a very common idiom to load a template, fill a context and return an
``HttpResponse`` object with the result of the rendered template. Django
provides a shortcut. Here's the full ``index()`` view, rewritten::
- from django.shortcuts import render_to_response
- from myproject.polls.models import Poll
+ from django.shortcuts import render_to_response
+ from myproject.polls.models import Poll
- def index(request):
- latest_poll_list = Poll.objects.all().order_by('-pub_date')
+ def index(request):
+ latest_poll_list = Poll.objects.all().order_by('-pub_date')
return render_to_response('polls/index', {'latest_poll_list': latest_poll_list})
Note that we no longer need to import ``loader``, ``Context`` or
@@ -278,12 +278,12 @@ for a given poll. Here's the view::
from django.http import Http404
# ...
- def detail(request, poll_id):
- try:
- p = Poll.objects.get(pk=poll_id)
- except Poll.DoesNotExist:
- raise Http404
- return render_to_response('polls/detail', {'poll': p})
+ def detail(request, poll_id):
+ try:
+ p = Poll.objects.get(pk=poll_id)
+ except Poll.DoesNotExist:
+ raise Http404
+ return render_to_response('polls/detail', {'poll': p})
The new concept here: The view raises the ``django.http.Http404``
exception if a poll with the requested ID doesn't exist.