[1.3.X] Fixed #17171 -- Updated tutorial urls.py code snippets to match startproject template and recommended practice. (No 'import *', use 'urls()' function.). Thanks haimunt for report.

Parts of this are a backport of r17073 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.3.X@17074 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Carl Meyer 2011-11-06 16:59:26 +00:00
parent 6372368b56
commit eabbb361d2
3 changed files with 43 additions and 43 deletions

View File

@ -40,22 +40,22 @@ activate the admin site for your installation, do these three things:
.. parsed-literal::
from django.conf.urls.defaults import *
from django.conf.urls.defaults import patterns, include, url
# Uncomment the next two lines to enable the admin:
**from django.contrib import admin**
**admin.autodiscover()**
urlpatterns = patterns('',
# Example:
# (r'^mysite/', include('mysite.foo.urls')),
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^mysite/', include('mysite.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
**(r'^admin/', include(admin.site.urls)),**
**url(r'^admin/', include(admin.site.urls)),**
)
(The bold lines are the ones that needed to be uncommented.)

View File

@ -78,17 +78,17 @@ point at that file::
Time for an example. Edit ``mysite/urls.py`` so it looks like this::
from django.conf.urls.defaults import *
from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(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)),
url(r'^polls/$', 'polls.views.index'),
url(r'^polls/(?P<poll_id>\d+)/$', 'polls.views.detail'),
url(r'^polls/(?P<poll_id>\d+)/results/$', 'polls.views.results'),
url(r'^polls/(?P<poll_id>\d+)/vote/$', 'polls.views.vote'),
url(r'^admin/', include(admin.site.urls)),
)
This is worth a review. When somebody requests a page from your Web site -- say,
@ -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$', 'polls.views.index'),
url(r'^polls/latest\.php$', 'polls.views.index'),
But, don't do that. It's silly.
@ -434,10 +434,10 @@ 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/$', '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'),
url(r'^polls/$', 'polls.views.index'),
url(r'^polls/(?P<poll_id>\d+)/$', 'polls.views.detail'),
url(r'^polls/(?P<poll_id>\d+)/results/$', 'polls.views.results'),
url(r'^polls/(?P<poll_id>\d+)/vote/$', 'polls.views.vote'),
)
Namely, ``polls.views`` is in every callback.
@ -447,10 +447,10 @@ 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('polls.views',
(r'^polls/$', 'index'),
(r'^polls/(?P<poll_id>\d+)/$', 'detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'vote'),
url(r'^polls/$', 'index'),
url(r'^polls/(?P<poll_id>\d+)/$', 'detail'),
url(r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
url(r'^polls/(?P<poll_id>\d+)/vote/$', 'vote'),
)
This is functionally identical to the previous formatting. It's just a bit
@ -461,20 +461,20 @@ callback in your URLconf, you can concatenate multiple
:func:`~django.conf.urls.defaults.patterns`. Your full ``mysite/urls.py`` might
now look like this::
from django.conf.urls.defaults import *
from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('polls.views',
(r'^polls/$', 'index'),
(r'^polls/(?P<poll_id>\d+)/$', 'detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'vote'),
url(r'^polls/$', 'index'),
url(r'^polls/(?P<poll_id>\d+)/$', 'detail'),
url(r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
url(r'^polls/(?P<poll_id>\d+)/vote/$', 'vote'),
)
urlpatterns += patterns('',
(r'^admin/', include(admin.site.urls)),
url(r'^admin/', include(admin.site.urls)),
)
Decoupling the URLconfs
@ -504,8 +504,8 @@ Copy the file ``mysite/urls.py`` to ``polls/urls.py``. Then, change
admin.autodiscover()
urlpatterns = patterns('',
(r'^polls/', include('polls.urls')),
(r'^admin/', include(admin.site.urls)),
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
:func:`~django.conf.urls.defaults.include` simply references another URLconf.
@ -528,13 +528,13 @@ URLconf by removing the leading "polls/" from each line, and removing the
lines registering the admin site. Your ``polls/urls.py`` file should now look like
this::
from django.conf.urls.defaults import *
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('polls.views',
(r'^$', 'index'),
(r'^(?P<poll_id>\d+)/$', 'detail'),
(r'^(?P<poll_id>\d+)/results/$', 'results'),
(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
url(r'^$', 'index'),
url(r'^(?P<poll_id>\d+)/$', 'detail'),
url(r'^(?P<poll_id>\d+)/results/$', 'results'),
url(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
The idea behind :func:`~django.conf.urls.defaults.include` and URLconf

View File

@ -218,13 +218,13 @@ Read on for details.
First, open the ``polls/urls.py`` URLconf. It looks like this, according to the
tutorial so far::
from django.conf.urls.defaults import *
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('polls.views',
(r'^$', 'index'),
(r'^(?P<poll_id>\d+)/$', 'detail'),
(r'^(?P<poll_id>\d+)/results/$', 'results'),
(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
url(r'^$', 'index'),
url(r'^(?P<poll_id>\d+)/$', 'detail'),
url(r'^(?P<poll_id>\d+)/results/$', 'results'),
url(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
Change it like so::
@ -234,12 +234,12 @@ Change it like so::
from polls.models import Poll
urlpatterns = patterns('',
(r'^$',
url(r'^$',
ListView.as_view(
queryset=Poll.objects.order_by('-pub_date')[:5],
context_object_name='latest_poll_list',
template_name='polls/index.html')),
(r'^(?P<pk>\d+)/$',
url(r'^(?P<pk>\d+)/$',
DetailView.as_view(
model=Poll,
template_name='polls/detail.html')),
@ -248,7 +248,7 @@ Change it like so::
model=Poll,
template_name='polls/results.html'),
name='poll_results'),
(r'^(?P<poll_id>\d+)/vote/$', 'polls.views.vote'),
url(r'^(?P<poll_id>\d+)/vote/$', 'polls.views.vote'),
)
We're using two generic views here: