mirror of
https://github.com/django/django.git
synced 2025-07-05 02:09:13 +00:00
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
This commit is contained in:
parent
d1083f15fb
commit
f576187df6
@ -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
|
latest 5 poll questions in the system, separated by commas, according to
|
||||||
publication date::
|
publication date::
|
||||||
|
|
||||||
from myproject.polls.models import Poll
|
from myproject.polls.models import Poll
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
|
|
||||||
def index(request):
|
def index(request):
|
||||||
latest_poll_list = Poll.objects.all().order_by('-pub_date')
|
latest_poll_list = Poll.objects.all().order_by('-pub_date')
|
||||||
output = ', '.join([p.question for p in latest_poll_list])
|
output = ', '.join([p.question for p in latest_poll_list])
|
||||||
return HttpResponse(output)
|
return HttpResponse(output)
|
||||||
|
|
||||||
There's a problem here, though: The page's design is hard-coded in the view. If
|
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.
|
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::
|
So let's use Django's template system to separate the design from Python::
|
||||||
|
|
||||||
from django.template import Context, loader
|
from django.template import Context, loader
|
||||||
from myproject.polls.models import Poll
|
from myproject.polls.models import Poll
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
|
|
||||||
def index(request):
|
def index(request):
|
||||||
latest_poll_list = Poll.objects.all().order_by('-pub_date')
|
latest_poll_list = Poll.objects.all().order_by('-pub_date')
|
||||||
t = loader.get_template('polls/index')
|
t = loader.get_template('polls/index')
|
||||||
c = Context({
|
c = Context({
|
||||||
'latest_poll_list': latest_poll_list,
|
'latest_poll_list': latest_poll_list,
|
||||||
})
|
})
|
||||||
return HttpResponse(t.render(c))
|
return HttpResponse(t.render(c))
|
||||||
|
|
||||||
That code loads the template called "polls/index" and passes it a context. The
|
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.
|
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
|
``HttpResponse`` object with the result of the rendered template. Django
|
||||||
provides a shortcut. Here's the full ``index()`` view, rewritten::
|
provides a shortcut. Here's the full ``index()`` view, rewritten::
|
||||||
|
|
||||||
from django.shortcuts import render_to_response
|
from django.shortcuts import render_to_response
|
||||||
from myproject.polls.models import Poll
|
from myproject.polls.models import Poll
|
||||||
|
|
||||||
def index(request):
|
def index(request):
|
||||||
latest_poll_list = Poll.objects.all().order_by('-pub_date')
|
latest_poll_list = Poll.objects.all().order_by('-pub_date')
|
||||||
return render_to_response('polls/index', {'latest_poll_list': latest_poll_list})
|
return render_to_response('polls/index', {'latest_poll_list': latest_poll_list})
|
||||||
|
|
||||||
Note that we no longer need to import ``loader``, ``Context`` or
|
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
|
from django.http import Http404
|
||||||
# ...
|
# ...
|
||||||
def detail(request, poll_id):
|
def detail(request, poll_id):
|
||||||
try:
|
try:
|
||||||
p = Poll.objects.get(pk=poll_id)
|
p = Poll.objects.get(pk=poll_id)
|
||||||
except Poll.DoesNotExist:
|
except Poll.DoesNotExist:
|
||||||
raise Http404
|
raise Http404
|
||||||
return render_to_response('polls/detail', {'poll': p})
|
return render_to_response('polls/detail', {'poll': p})
|
||||||
|
|
||||||
The new concept here: The view raises the ``django.http.Http404``
|
The new concept here: The view raises the ``django.http.Http404``
|
||||||
exception if a poll with the requested ID doesn't exist.
|
exception if a poll with the requested ID doesn't exist.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user