diff options
| author | Simon Meers <simon@simonmeers.com> | 2010-10-09 07:45:52 +0000 |
|---|---|---|
| committer | Simon Meers <simon@simonmeers.com> | 2010-10-09 07:45:52 +0000 |
| commit | d81faf356c53c0cf099d48551e1d3982135f2db9 (patch) | |
| tree | 1164fc277ebe85e662d100452412ca2aa8f10459 /docs/intro/tutorial03.txt | |
| parent | 307e71a734a0542020c87f4589df7a182ee29606 (diff) | |
Fixed #14255 -- factor project name out of app imports in tutorial. Thanks to adamend for the report and initial patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14066 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'docs/intro/tutorial03.txt')
| -rw-r--r-- | docs/intro/tutorial03.txt | 48 |
1 files changed, 24 insertions, 24 deletions
diff --git a/docs/intro/tutorial03.txt b/docs/intro/tutorial03.txt index d3470359aa..6da0ad4029 100644 --- a/docs/intro/tutorial03.txt +++ b/docs/intro/tutorial03.txt @@ -84,10 +84,10 @@ Time for an example. Edit ``mysite/urls.py`` so it looks like this:: admin.autodiscover() urlpatterns = patterns('', - (r'^polls/$', 'mysite.polls.views.index'), - (r'^polls/(?P<poll_id>\d+)/$', 'mysite.polls.views.detail'), - (r'^polls/(?P<poll_id>\d+)/results/$', 'mysite.polls.views.results'), - (r'^polls/(?P<poll_id>\d+)/vote/$', 'mysite.polls.views.vote'), + (r'^polls/$', 'polls.views.index'), + (r'^polls/(?P<poll_id>\d+)/$', 'polls.views.detail'), + (r'^polls/(?P<poll_id>\d+)/results/$', 'polls.views.results'), + (r'^polls/(?P<poll_id>\d+)/vote/$', 'polls.views.vote'), (r'^admin/', include(admin.site.urls)), ) @@ -96,8 +96,8 @@ 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 -function ``detail()`` from ``mysite/polls/views.py``. Finally, -it calls that ``detail()`` function like so:: +function ``detail()`` from ``polls/views.py``. Finally, it calls that +``detail()`` function like so:: detail(request=<HttpRequest object>, poll_id='23') @@ -112,7 +112,7 @@ what you can do with them. And there's no need to add URL cruft such as ``.php`` -- unless you have a sick sense of humor, in which case you can do something like this:: - (r'^polls/latest\.php$', 'mysite.polls.views.index'), + (r'^polls/latest\.php$', 'polls.views.index'), But, don't do that. It's silly. @@ -148,17 +148,17 @@ You should get a pleasantly-colored error page with the following message:: ViewDoesNotExist at /polls/ - Tried index in module mysite.polls.views. Error was: 'module' + Tried index in module polls.views. Error was: 'module' object has no attribute 'index' This error happened because you haven't written a function ``index()`` in the -module ``mysite/polls/views.py``. +module ``polls/views.py``. Try "/polls/23/", "/polls/23/results/" and "/polls/23/vote/". The error messages tell you which view Django tried (and failed to find, because you haven't written any views yet). -Time to write the first view. Open the file ``mysite/polls/views.py`` +Time to write the first view. Open the file ``polls/views.py`` and put the following Python code in it:: from django.http import HttpResponse @@ -207,7 +207,7 @@ in :doc:`Tutorial 1 </intro/tutorial01>`. 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 mysite.polls.models import Poll + from polls.models import Poll from django.http import HttpResponse def index(request): @@ -220,7 +220,7 @@ 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 mysite.polls.models import Poll + from polls.models import Poll from django.http import HttpResponse def index(request): @@ -279,7 +279,7 @@ template. Django provides a shortcut. Here's the full ``index()`` view, rewritten:: from django.shortcuts import render_to_response - from mysite.polls.models import Poll + from polls.models import Poll def index(request): latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5] @@ -432,19 +432,19 @@ Take some time to play around with the views and template system. As you edit the URLconf, you may notice there's a fair bit of redundancy in it:: urlpatterns = patterns('', - (r'^polls/$', 'mysite.polls.views.index'), - (r'^polls/(?P<poll_id>\d+)/$', 'mysite.polls.views.detail'), - (r'^polls/(?P<poll_id>\d+)/results/$', 'mysite.polls.views.results'), - (r'^polls/(?P<poll_id>\d+)/vote/$', 'mysite.polls.views.vote'), + (r'^polls/$', 'polls.views.index'), + (r'^polls/(?P<poll_id>\d+)/$', 'polls.views.detail'), + (r'^polls/(?P<poll_id>\d+)/results/$', 'polls.views.results'), + (r'^polls/(?P<poll_id>\d+)/vote/$', 'polls.views.vote'), ) -Namely, ``mysite.polls.views`` is in every callback. +Namely, ``polls.views`` is in every callback. Because this is a common case, the URLconf framework provides a shortcut for common prefixes. You can factor out the common prefixes and add them as the first argument to :func:`~django.conf.urls.defaults.patterns`, like so:: - urlpatterns = patterns('mysite.polls.views', + urlpatterns = patterns('polls.views', (r'^polls/$', 'index'), (r'^polls/(?P<poll_id>\d+)/$', 'detail'), (r'^polls/(?P<poll_id>\d+)/results/$', 'results'), @@ -470,7 +470,7 @@ We've been editing the URLs in ``mysite/urls.py``, but the URL design of an app is specific to the app, not to the Django installation -- so let's move the URLs within the app directory. -Copy the file ``mysite/urls.py`` to ``mysite/polls/urls.py``. Then, change +Copy the file ``mysite/urls.py`` to ``polls/urls.py``. Then, change ``mysite/urls.py`` to remove the poll-specific URLs and insert an :func:`~django.conf.urls.defaults.include`:: @@ -479,7 +479,7 @@ Copy the file ``mysite/urls.py`` to ``mysite/polls/urls.py``. Then, change # ... urlpatterns = patterns('', - (r'^polls/', include('mysite.polls.urls')), + (r'^polls/', include('polls.urls')), # ... ) @@ -495,14 +495,14 @@ Here's what happens if a user goes to "/polls/34/" in this system: * Django will find the match at ``'^polls/'`` * Then, Django will strip off the matching text (``"polls/"``) and send the - remaining text -- ``"34/"`` -- to the 'mysite.polls.urls' URLconf for + remaining text -- ``"34/"`` -- to the 'polls.urls' URLconf for further processing. -Now that we've decoupled that, we need to decouple the 'mysite.polls.urls' +Now that we've decoupled that, we need to decouple the 'polls.urls' URLconf by removing the leading "polls/" from each line, and removing the lines registering the admin site:: - urlpatterns = patterns('mysite.polls.views', + urlpatterns = patterns('polls.views', (r'^$', 'index'), (r'^(?P<poll_id>\d+)/$', 'detail'), (r'^(?P<poll_id>\d+)/results/$', 'results'), |
