mirror of
https://github.com/django/django.git
synced 2025-08-03 18:38:50 +00:00
Fixed #22378 -- Updated \d to [0-9]+ in urlpatterns of docs and tests.
Thanks tomwys for the suggestion.
This commit is contained in:
parent
030dd4f72c
commit
66ec9ee441
38 changed files with 181 additions and 181 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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'),
|
||||
]
|
||||
|
||||
|
|
|
@ -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"),
|
||||
]
|
||||
|
|
|
@ -217,7 +217,7 @@ The police beat feeds could be accessible via URLs like this:
|
|||
|
||||
These can be matched with a :doc:`URLconf </topics/http/urls>` line such as::
|
||||
|
||||
(r'^beats/(?P<beat_id>\d+)/rss/$', BeatFeed()),
|
||||
(r'^beats/(?P<beat_id>[0-9]+)/rss/$', BeatFeed()),
|
||||
|
||||
Like a view, the arguments in the URL are passed to the ``get_object()``
|
||||
method along with the request object.
|
||||
|
|
|
@ -930,10 +930,10 @@ Slightly complex built-in ``Field`` classes
|
|||
# Or define a different message for each field.
|
||||
fields = (
|
||||
CharField(error_messages={'incomplete': 'Enter a country code.'},
|
||||
validators=[RegexValidator(r'^\d+$', 'Enter a valid country code.')]),
|
||||
validators=[RegexValidator(r'^[0-9]+$', 'Enter a valid country code.')]),
|
||||
CharField(error_messages={'incomplete': 'Enter a phone number.'},
|
||||
validators=[RegexValidator(r'^\d+$', 'Enter a valid phone number.')]),
|
||||
CharField(validators=[RegexValidator(r'^\d+$', 'Enter a valid extension.')],
|
||||
validators=[RegexValidator(r'^[0-9]+$', 'Enter a valid phone number.')]),
|
||||
CharField(validators=[RegexValidator(r'^[0-9]+$', 'Enter a valid extension.')],
|
||||
required=False),
|
||||
)
|
||||
super(PhoneField, self).__init__(
|
||||
|
|
|
@ -642,7 +642,7 @@ template tag and a high-level wrapper for the
|
|||
An example should make it clear how to use ``permalink()``. Suppose your URLconf
|
||||
contains a line such as::
|
||||
|
||||
(r'^people/(\d+)/$', 'people.views.details'),
|
||||
(r'^people/([0-9]+)/$', 'people.views.details'),
|
||||
|
||||
...your model could have a :meth:`~django.db.models.Model.get_absolute_url`
|
||||
method that looked like this::
|
||||
|
@ -655,7 +655,7 @@ method that looked like this::
|
|||
|
||||
Similarly, if you had a URLconf entry that looked like::
|
||||
|
||||
(r'/archive/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/$', archive_view)
|
||||
(r'/archive/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', archive_view)
|
||||
|
||||
...you could reference this using ``permalink()`` as follows::
|
||||
|
||||
|
@ -684,7 +684,7 @@ pattern tuple by a call to the ``url`` function)::
|
|||
|
||||
from django.conf.urls import url
|
||||
|
||||
url(r'^people/(\d+)/$', 'blog_views.generic_detail', name='people_view'),
|
||||
url(r'^people/([0-9]+)/$', 'blog_views.generic_detail', name='people_view'),
|
||||
|
||||
...and then using that name to perform the reverse URL resolution instead
|
||||
of the view name::
|
||||
|
|
|
@ -1000,7 +1000,7 @@ takes a client ID (here, ``client()`` is a method inside the views file
|
|||
|
||||
.. code-block:: python
|
||||
|
||||
('^client/(\d+)/$', 'app_views.client')
|
||||
('^client/([0-9]+)/$', 'app_views.client')
|
||||
|
||||
If this app's URLconf is included into the project's URLconf under a path
|
||||
such as this:
|
||||
|
|
|
@ -23,9 +23,9 @@ URLconf from the :doc:`Django overview </intro/overview>`::
|
|||
from django.conf.urls import patterns, url
|
||||
|
||||
urlpatterns = patterns('',
|
||||
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'),
|
||||
)
|
||||
|
||||
In this example, each view has a common prefix -- ``'news.views'``.
|
||||
|
@ -38,9 +38,9 @@ With this in mind, the above example can be written more concisely as::
|
|||
from django.conf.urls import patterns, url
|
||||
|
||||
urlpatterns = patterns('news.views',
|
||||
url(r'^articles/(\d{4})/$', 'year_archive'),
|
||||
url(r'^articles/(\d{4})/(\d{2})/$', 'month_archive'),
|
||||
url(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'article_detail'),
|
||||
url(r'^articles/([0-9]{4})/$', 'year_archive'),
|
||||
url(r'^articles/([0-9]{4})/([0-9]{2})/$', 'month_archive'),
|
||||
url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', 'article_detail'),
|
||||
)
|
||||
|
||||
Note that you don't put a trailing dot (``"."``) in the prefix. Django puts
|
||||
|
|
|
@ -533,7 +533,7 @@ multiple URLs point at the same view, each URL will be cached separately.
|
|||
Continuing the ``my_view`` example, if your URLconf looks like this::
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^foo/(\d{1,2})/$', my_view),
|
||||
url(r'^foo/([0-9]{1,2})/$', my_view),
|
||||
]
|
||||
|
||||
then requests to ``/foo/1/`` and ``/foo/23/`` will be cached separately, as
|
||||
|
@ -579,7 +579,7 @@ Doing so is easy: simply wrap the view function with ``cache_page`` when you
|
|||
refer to it in the URLconf. Here's the old URLconf from earlier::
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^foo/(\d{1,2})/$', my_view),
|
||||
url(r'^foo/([0-9]{1,2})/$', my_view),
|
||||
]
|
||||
|
||||
Here's the same thing, with ``my_view`` wrapped in ``cache_page``::
|
||||
|
@ -587,7 +587,7 @@ Here's the same thing, with ``my_view`` wrapped in ``cache_page``::
|
|||
from django.views.decorators.cache import cache_page
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^foo/(\d{1,2})/$', cache_page(60 * 15)(my_view)),
|
||||
url(r'^foo/([0-9]{1,2})/$', cache_page(60 * 15)(my_view)),
|
||||
]
|
||||
|
||||
.. templatetag:: cache
|
||||
|
|
|
@ -401,7 +401,7 @@ custom view::
|
|||
|
||||
urlpatterns = [
|
||||
#...
|
||||
url(r'^authors/(?P<pk>\d+)/$', AuthorDetailView.as_view(), name='author-detail'),
|
||||
url(r'^authors/(?P<pk>[0-9]+)/$', AuthorDetailView.as_view(), name='author-detail'),
|
||||
]
|
||||
|
||||
Then we'd write our new view -- ``get_object`` is the method that retrieves the
|
||||
|
|
|
@ -147,8 +147,8 @@ Finally, we hook these new views into the URLconf::
|
|||
urlpatterns = [
|
||||
# ...
|
||||
url(r'author/add/$', AuthorCreate.as_view(), name='author_add'),
|
||||
url(r'author/(?P<pk>\d+)/$', AuthorUpdate.as_view(), name='author_update'),
|
||||
url(r'author/(?P<pk>\d+)/delete/$', AuthorDelete.as_view(), name='author_delete'),
|
||||
url(r'author/(?P<pk>[0-9]+)/$', AuthorUpdate.as_view(), name='author_update'),
|
||||
url(r'author/(?P<pk>[0-9]+)/delete/$', AuthorDelete.as_view(), name='author_delete'),
|
||||
]
|
||||
|
||||
.. note::
|
||||
|
|
|
@ -261,7 +261,7 @@ We can hook this into our URLs easily enough::
|
|||
|
||||
urlpatterns = [
|
||||
#...
|
||||
url(r'^author/(?P<pk>\d+)/interest/$', RecordInterest.as_view(), name='author-interest'),
|
||||
url(r'^author/(?P<pk>[0-9]+)/interest/$', RecordInterest.as_view(), name='author-interest'),
|
||||
]
|
||||
|
||||
Note the ``pk`` named group, which
|
||||
|
|
|
@ -76,9 +76,9 @@ Here's a sample URLconf::
|
|||
|
||||
urlpatterns = [
|
||||
url(r'^articles/2003/$', 'news.views.special_case_2003'),
|
||||
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'),
|
||||
]
|
||||
|
||||
Notes:
|
||||
|
@ -133,9 +133,9 @@ Here's the above example URLconf, rewritten to use named groups::
|
|||
|
||||
urlpatterns = [
|
||||
url(r'^articles/2003/$', 'news.views.special_case_2003'),
|
||||
url(r'^articles/(?P<year>\d{4})/$', 'news.views.year_archive'),
|
||||
url(r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'news.views.month_archive'),
|
||||
url(r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/$', 'news.views.article_detail'),
|
||||
url(r'^articles/(?P<year>[0-9]{4})/$', 'news.views.year_archive'),
|
||||
url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', 'news.views.month_archive'),
|
||||
url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', 'news.views.article_detail'),
|
||||
]
|
||||
|
||||
This accomplishes exactly the same thing as the previous example, with one
|
||||
|
@ -191,10 +191,10 @@ Each captured argument is sent to the view as a plain Python string, regardless
|
|||
of what sort of match the regular expression makes. For example, in this
|
||||
URLconf line::
|
||||
|
||||
url(r'^articles/(?P<year>\d{4})/$', 'news.views.year_archive'),
|
||||
url(r'^articles/(?P<year>[0-9]{4})/$', 'news.views.year_archive'),
|
||||
|
||||
...the ``year`` argument to ``news.views.year_archive()`` will be a string, not
|
||||
an integer, even though the ``\d{4}`` will only match integer strings.
|
||||
an integer, even though the ``[0-9]{4}`` will only match integer strings.
|
||||
|
||||
Specifying defaults for view arguments
|
||||
======================================
|
||||
|
@ -207,7 +207,7 @@ Here's an example URLconf and view::
|
|||
|
||||
urlpatterns = [
|
||||
url(r'^blog/$', 'blog.views.page'),
|
||||
url(r'^blog/page(?P<num>\d+)/$', 'blog.views.page'),
|
||||
url(r'^blog/page(?P<num>[0-9]+)/$', 'blog.views.page'),
|
||||
]
|
||||
|
||||
# View (in blog/views.py)
|
||||
|
@ -291,7 +291,7 @@ Another possibility is to include additional URL patterns by using a list of
|
|||
from django.conf.urls import include, url
|
||||
|
||||
extra_patterns = [
|
||||
url(r'^reports/(?P<id>\d+)/$', 'credit.views.report'),
|
||||
url(r'^reports/(?P<id>[0-9]+)/$', 'credit.views.report'),
|
||||
url(r'^charge/$', 'credit.views.charge'),
|
||||
]
|
||||
|
||||
|
@ -377,7 +377,7 @@ For example::
|
|||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^blog/(?P<year>\d{4})/$', views.year_archive, {'foo': 'bar'}),
|
||||
url(r'^blog/(?P<year>[0-9]{4})/$', views.year_archive, {'foo': 'bar'}),
|
||||
]
|
||||
|
||||
In this example, for a request to ``/blog/2005/``, Django will call
|
||||
|
@ -558,7 +558,7 @@ Consider again this URLconf entry::
|
|||
|
||||
urlpatterns = [
|
||||
#...
|
||||
url(r'^articles/(\d{4})/$', 'news.views.year_archive'),
|
||||
url(r'^articles/([0-9]{4})/$', 'news.views.year_archive'),
|
||||
#...
|
||||
]
|
||||
|
||||
|
@ -610,8 +610,8 @@ view::
|
|||
from mysite.views import archive
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^archive/(\d{4})/$', archive),
|
||||
url(r'^archive-summary/(\d{4})/$', archive, {'summary': True}),
|
||||
url(r'^archive/([0-9]{4})/$', archive),
|
||||
url(r'^archive-summary/([0-9]{4})/$', archive, {'summary': True}),
|
||||
]
|
||||
|
||||
This is completely valid, but it leads to problems when you try to do reverse
|
||||
|
@ -631,8 +631,8 @@ Here's the above example, rewritten to use named URL patterns::
|
|||
from mysite.views import archive
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^archive/(\d{4})/$', archive, name="full-archive"),
|
||||
url(r'^archive-summary/(\d{4})/$', archive, {'summary': True}, name="arch-summary"),
|
||||
url(r'^archive/([0-9]{4})/$', archive, name="full-archive"),
|
||||
url(r'^archive-summary/([0-9]{4})/$', archive, {'summary': True}, name="arch-summary"),
|
||||
]
|
||||
|
||||
With these names in place (``full-archive`` and ``arch-summary``), you can
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue