Commit 66ec9ee4 authored by chriscauley's avatar chriscauley Committed by Tim Graham
Browse files

Fixed #22378 -- Updated \d to [0-9]+ in urlpatterns of docs and tests.

Thanks tomwys for the suggestion.
parent 030dd4f7
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -187,9 +187,9 @@ example above::
    from django.conf.urls import url

    urlpatterns = [
        url(r'^articles/(\d{4})/$', 'news.views.year_archive'),
        url(r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'),
        url(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'),
        url(r'^articles/([0-9]{4})/$', 'news.views.year_archive'),
        url(r'^articles/([0-9]{4})/([0-9]{2})/$', 'news.views.month_archive'),
        url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', 'news.views.article_detail'),
    ]

The code above maps URLs, as simple `regular expressions`_, to the location of
+8 −8
Original line number Diff line number Diff line
@@ -215,11 +215,11 @@ Wire these new views into the ``polls.urls`` module by adding the following
        # ex: /polls/
        url(r'^$', views.index, name='index'),
        # ex: /polls/5/
        url(r'^(?P<question_id>\d+)/$', views.detail, name='detail'),
        url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
        # ex: /polls/5/results/
        url(r'^(?P<question_id>\d+)/results/$', views.results, name='results'),
        url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
        # ex: /polls/5/vote/
        url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
        url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
    ]

Take a look in your browser, at "/polls/34/". It'll run the ``detail()``
@@ -251,15 +251,15 @@ Here's what happens if a user goes to "/polls/34/" in this system:

* Then, Django will strip off the matching text (``"polls/"``) and send the
  remaining text -- ``"34/"`` -- to the 'polls.urls' URLconf for
  further processing which matches ``r'^(?P<question_id>\d+)/$'`` resulting in a
  further processing which matches ``r'^(?P<question_id>[0-9]+)/$'`` resulting in a
  call to the ``detail()`` view like so::

    detail(request=<HttpRequest object>, question_id='34')

The ``question_id='34'`` part comes from ``(?P<question_id>\d+)``. Using parentheses
The ``question_id='34'`` part comes from ``(?P<question_id>[0-9]+)``. Using parentheses
around a pattern "captures" the text matched by that pattern and sends it as an
argument to the view function; ``?P<question_id>`` defines the name that will
be used to identify the matched pattern; and ``\d+`` is a regular expression to
be used to identify the matched pattern; and ``[0-9]+`` is a regular expression to
match a sequence of digits (i.e., a number).

Because the URL patterns are regular expressions, there really is no limit on
@@ -554,7 +554,7 @@ defined below::

    ...
    # the 'name' value as called by the {% url %} template tag
    url(r'^(?P<question_id>\d+)/$', views.detail, name='detail'),
    url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
    ...

If you want to change the URL of the polls detail view to something else,
@@ -563,7 +563,7 @@ template (or templates) you would change it in ``polls/urls.py``::

    ...
    # added the word 'specifics'
    url(r'^specifics/(?P<question_id>\d+)/$', views.detail, name='detail'),
    url(r'^specifics/(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
    ...

Namespacing URL names
+4 −4
Original line number Diff line number Diff line
@@ -61,7 +61,7 @@ created a URLconf for the polls application that includes this line:
.. snippet::
    :filename: polls/urls.py

    url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),

We also created a dummy implementation of the ``vote()`` function. Let's
create a real version. Add the following to ``polls/views.py``:
@@ -228,9 +228,9 @@ First, open the ``polls/urls.py`` URLconf and change it like so:

    urlpatterns = [
        url(r'^$', views.IndexView.as_view(), name='index'),
        url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
        url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
        url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
        url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
        url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
        url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
    ]

Amend views
+2 −2
Original line number Diff line number Diff line
@@ -198,8 +198,8 @@ RedirectView
        from article.views import ArticleCounterRedirectView, ArticleDetail

        urlpatterns = [
            url(r'^counter/(?P<pk>\d+)/$', ArticleCounterRedirectView.as_view(), name='article-counter'),
            url(r'^details/(?P<pk>\d+)/$', ArticleDetail.as_view(), name='article-detail'),
            url(r'^counter/(?P<pk>[0-9]+)/$', ArticleCounterRedirectView.as_view(), name='article-counter'),
            url(r'^details/(?P<pk>[0-9]+)/$', ArticleDetail.as_view(), name='article-detail'),
            url(r'^go-to-django/$', RedirectView.as_view(url='http://djangoproject.com'), name='go-to-django'),
        ]

+6 −6
Original line number Diff line number Diff line
@@ -171,7 +171,7 @@ YearArchiveView
        from myapp.views import ArticleYearArchiveView

        urlpatterns = [
            url(r'^(?P<year>\d{4})/$',
            url(r'^(?P<year>[0-9]{4})/$',
                ArticleYearArchiveView.as_view(),
                name="article_year_archive"),
        ]
@@ -267,11 +267,11 @@ MonthArchiveView

        urlpatterns = [
            # Example: /2012/aug/
            url(r'^(?P<year>\d{4})/(?P<month>[-\w]+)/$',
            url(r'^(?P<year>[0-9]{4})/(?P<month>[-\w]+)/$',
                ArticleMonthArchiveView.as_view(),
                name="archive_month"),
            # Example: /2012/08/
            url(r'^(?P<year>\d{4})/(?P<month>\d+)/$',
            url(r'^(?P<year>[0-9]{4})/(?P<month>[0-9]+)/$',
                ArticleMonthArchiveView.as_view(month_format='%m'),
                name="archive_month_numeric"),
        ]
@@ -361,7 +361,7 @@ WeekArchiveView

        urlpatterns = [
            # Example: /2012/week/23/
            url(r'^(?P<year>\d{4})/week/(?P<week>\d+)/$',
            url(r'^(?P<year>[0-9]{4})/week/(?P<week>[0-9]+)/$',
                ArticleWeekArchiveView.as_view(),
                name="archive_week"),
        ]
@@ -475,7 +475,7 @@ DayArchiveView

        urlpatterns = [
            # Example: /2012/nov/10/
            url(r'^(?P<year>\d{4})/(?P<month>[-\w]+)/(?P<day>\d+)/$',
            url(r'^(?P<year>[0-9]{4})/(?P<month>[-\w]+)/(?P<day>[0-9]+)/$',
                ArticleDayArchiveView.as_view(),
                name="archive_day"),
        ]
@@ -597,7 +597,7 @@ DateDetailView
        from django.views.generic.dates import DateDetailView

        urlpatterns = [
            url(r'^(?P<year>\d+)/(?P<month>[-\w]+)/(?P<day>\d+)/(?P<pk>\d+)/$',
            url(r'^(?P<year>[0-9]+)/(?P<month>[-\w]+)/(?P<day>[0-9]+)/(?P<pk>[0-9]+)/$',
                DateDetailView.as_view(model=Article, date_field="pub_date"),
                name="archive_date_detail"),
        ]
Loading