diff options
Diffstat (limited to 'docs/tutorial03.txt')
| -rw-r--r-- | docs/tutorial03.txt | 72 |
1 files changed, 38 insertions, 34 deletions
diff --git a/docs/tutorial03.txt b/docs/tutorial03.txt index e7cc9f7161..b26614a5ee 100644 --- a/docs/tutorial03.txt +++ b/docs/tutorial03.txt @@ -74,25 +74,27 @@ Time for an example. Edit ``myproject/urls.py`` so it looks like this:: urlpatterns = patterns('', (r'^polls/$', 'myproject.polls.views.index'), - (r'^polls/(\d+)/$', 'myproject.polls.views.detail'), - (r'^polls/(\d+)/results/$', 'myproject.polls.views.results'), - (r'^polls/(\d+)/vote/$', 'myproject.polls.views.vote'), + (r'^polls/(?P<poll_id>\d+)/$', 'myproject.polls.views.detail'), + (r'^polls/(?P<poll_id>\d+)/results/$', 'myproject.polls.views.results'), + (r'^polls/(?P<poll_id>\d+)/vote/$', 'myproject.polls.views.vote'), ) This is worth a review. When somebody requests a page from your Web site -- say, "/polls/23/", Django will load this Python module, because it's pointed to by the ``ROOT_URLCONF`` setting. It finds the variable named ``urlpatterns`` and traverses the regular expressions in order. When it finds a regular -expression that matches -- ``r'^polls/(\d+)/$'`` -- it loads the +expression that matches -- ``r'^polls/(?P<poll_id>\d+)/$'`` -- it loads the associated Python package/module: ``myproject.polls.views.detail``. That corresponds to the function ``detail()`` in ``myproject/polls/views.py``. Finally, it calls that ``detail()`` function like so:: detail(request=<HttpRequest object>, poll_id='23') -The ``poll_id='23'`` part comes from ``(\d+)``. Using parenthesis around a +The ``poll_id='23'`` part comes from ``(?P<poll_id>\d+)``. Using parenthesis around a pattern "captures" the text matched by that pattern and sends it as an argument -to the view function. +to the view function; the ``?P<poll_id>`` defines the name that will be used to +identify the matched pattern; and \d+ is a regular experession to match a sequence of +digits (i.e., a number). Because the URL patterns are regular expressions, there really is no limit on what you can do with them. And there's no need to add URL cruft such as @@ -185,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 django.models.polls import polls - from django.http import HttpResponse + from myproject.polls.models import Poll + from django.http import HttpResponse - def index(request): - latest_poll_list = polls.get_list(order_by=['-pub_date'], limit=5) - 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 django.models.polls import polls - 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 = polls.get_list(order_by=['-pub_date'], limit=5) - 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. @@ -254,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 django.models.polls import polls + from django.shortcuts import render_to_response + from myproject.polls.models import Poll - def index(request): - latest_poll_list = polls.get_list(order_by=['-pub_date'], limit=5) + 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 @@ -275,12 +277,13 @@ Now, let's tackle the poll detail view -- the page that displays the question for a given poll. Here's the view:: from django.http import Http404 - def detail(request, poll_id): - try: - p = polls.get_object(pk=poll_id) - except polls.PollDoesNotExist: - 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. @@ -292,9 +295,10 @@ It's a very common idiom to use ``get_object()`` and raise ``Http404`` if the object doesn't exist. Django provides a shortcut. Here's the ``detail()`` view, rewritten:: - from django.shortcuts import get_object_or_404 + from django.shortcuts import render_to_response, get_object_or_404 + # ... def detail(request, poll_id): - p = get_object_or_404(polls, pk=poll_id) + p = get_object_or_404(Poll, pk=poll_id) return render_to_response('polls/detail', {'poll': p}) The ``get_object_or_404()`` function takes a Django model module as its first |
