Skip to content

Commit

Permalink
Refs #17171 -- Updated tutorial sections 3 and 4 to follow the startp…
Browse files Browse the repository at this point in the history
…roject template and consistently use url() function in URLconf. Thanks haimunt for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17073 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
carljm committed Nov 6, 2011
1 parent 5f64af9 commit 7f658a5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
43 changes: 21 additions & 22 deletions docs/intro/tutorial03.txt
Expand Up @@ -84,10 +84,10 @@ Time for an example. Edit ``mysite/urls.py`` so it looks like this::
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'),
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)),
)

Expand Down Expand Up @@ -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.
Expand All @@ -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.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
Expand All @@ -467,10 +467,10 @@ now look like this::
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('',
Expand All @@ -497,14 +497,13 @@ 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.include`, leaving you with::

# This also imports the include function
from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
(r'^polls/', include('polls.urls')),
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)

Expand All @@ -531,10 +530,10 @@ this::
from django.conf.urls 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.include` and URLconf
Expand Down
14 changes: 7 additions & 7 deletions docs/intro/tutorial04.txt
Expand Up @@ -221,10 +221,10 @@ tutorial so far::
from django.conf.urls 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::
Expand All @@ -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')),
Expand All @@ -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:
Expand Down

0 comments on commit 7f658a5

Please sign in to comment.