diff options
Diffstat (limited to 'docs/intro/tutorial03.txt')
| -rw-r--r-- | docs/intro/tutorial03.txt | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/docs/intro/tutorial03.txt b/docs/intro/tutorial03.txt index 53c2bcfcfc..f4ef5f76fe 100644 --- a/docs/intro/tutorial03.txt +++ b/docs/intro/tutorial03.txt @@ -16,28 +16,28 @@ a specific function and has a specific template. For example, in a weblog application, you might have the following views: * Blog homepage -- displays the latest few entries. - + * Entry "detail" page -- permalink page for a single entry. - + * Year-based archive page -- displays all months with entries in the given year. - + * Month-based archive page -- displays all days with entries in the given month. - + * Day-based archive page -- displays all entries in the given day. - + * Comment action -- handles posting comments to a given entry. In our poll application, we'll have the following four views: * Poll "archive" page -- displays the latest few polls. - + * Poll "detail" page -- displays a poll question, with no results but with a form to vote. - + * Poll "results" page -- displays results for a particular poll. - + * Vote action -- handles voting for a particular choice in a particular poll. @@ -71,7 +71,7 @@ For more on :class:`~django.http.HttpRequest` objects, see the :ref:`ref-request-response`. For more details on URLconfs, see the :ref:`topics-http-urls`. -When you ran ``python django-admin.py startproject mysite`` at the beginning of +When you ran ``django-admin.py startproject mysite`` at the beginning of Tutorial 1, it created a default URLconf in ``mysite/urls.py``. It also automatically set your :setting:`ROOT_URLCONF` setting (in ``settings.py``) to point at that file:: @@ -82,6 +82,9 @@ Time for an example. Edit ``mysite/urls.py`` so it looks like this:: from django.conf.urls.defaults import * + from django.contrib import admin + admin.autodiscover() + urlpatterns = patterns('', (r'^polls/$', 'mysite.polls.views.index'), (r'^polls/(?P<poll_id>\d+)/$', 'mysite.polls.views.detail'), @@ -95,8 +98,7 @@ This is worth a review. When somebody requests a page from your Web site -- say, the :setting:`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/(?P<poll_id>\d+)/$'`` -- it loads the -associated Python package/module: ``mysite.polls.views.detail``. That -corresponds to the function ``detail()`` in ``mysite/polls/views.py``. Finally, +function ``detail()`` from ``mysite/polls/views.py``. Finally, it calls that ``detail()`` function like so:: detail(request=<HttpRequest object>, poll_id='23') @@ -307,7 +309,7 @@ We'll discuss what you could put in that ``polls/detail.html`` template a bit later, but if you'd like to quickly get the above example working, just:: {{ poll }} - + will get you started for now. A shortcut: get_object_or_404() @@ -371,12 +373,12 @@ Three more things to note about 404 views: * The 404 view is also called if Django doesn't find a match after checking every regular expression in the URLconf. - + * If you don't define your own 404 view -- and simply use the default, which is recommended -- you still have one obligation: To create a ``404.html`` template in the root of your template directory. The default 404 view will use that template for all 404 errors. - + * If :setting:`DEBUG` is set to ``False`` (in your settings module) and if you didn't create a ``404.html`` file, an ``Http500`` is raised instead. So remember to create a ``404.html``. @@ -465,7 +467,10 @@ Copy the file ``mysite/urls.py`` to ``mysite/polls/urls.py``. Then, change ``mysite/urls.py`` to remove the poll-specific URLs and insert an :func:`~django.conf.urls.defaults.include`:: - (r'^polls/', include('mysite.polls.urls')), + ... + urlpatterns = patterns('', + (r'^polls/', include('mysite.polls.urls')), + ... :func:`~django.conf.urls.defaults.include`, simply, references another URLconf. Note that the regular expression doesn't have a ``$`` (end-of-string match @@ -483,7 +488,8 @@ Here's what happens if a user goes to "/polls/34/" in this system: further processing. Now that we've decoupled that, we need to decouple the 'mysite.polls.urls' -URLconf by removing the leading "polls/" from each line:: +URLconf by removing the leading "polls/" from each line, and removing the +lines registering the admin site:: urlpatterns = patterns('mysite.polls.views', (r'^$', 'index'), |
