mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #28593 -- Added a simplified URL routing syntax per DEP 0201.
Thanks Aymeric Augustin for shepherding the DEP and patch review. Thanks Marten Kenbeek and Tim Graham for contributing to the code. Thanks Tom Christie, Shai Berger, and Tim Graham for the docs.
This commit is contained in:
committed by
Tim Graham
parent
c4c128d67c
commit
df41b5a05d
@@ -40,12 +40,12 @@ MRO is an acronym for Method Resolution Order.
|
||||
|
||||
**Example urls.py**::
|
||||
|
||||
from django.conf.urls import url
|
||||
from django.urls import path
|
||||
|
||||
from myapp.views import MyView
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^mine/$', MyView.as_view(), name='my-view'),
|
||||
path('mine/', MyView.as_view(), name='my-view'),
|
||||
]
|
||||
|
||||
**Attributes**
|
||||
@@ -144,12 +144,12 @@ MRO is an acronym for Method Resolution Order.
|
||||
|
||||
**Example urls.py**::
|
||||
|
||||
from django.conf.urls import url
|
||||
from django.urls import path
|
||||
|
||||
from myapp.views import HomePageView
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^$', HomePageView.as_view(), name='home'),
|
||||
path('', HomePageView.as_view(), name='home'),
|
||||
]
|
||||
|
||||
**Context**
|
||||
@@ -208,15 +208,15 @@ MRO is an acronym for Method Resolution Order.
|
||||
|
||||
**Example urls.py**::
|
||||
|
||||
from django.conf.urls import url
|
||||
from django.urls import path
|
||||
from django.views.generic.base import RedirectView
|
||||
|
||||
from article.views import ArticleCounterRedirectView, ArticleDetail
|
||||
|
||||
urlpatterns = [
|
||||
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='https://djangoproject.com'), name='go-to-django'),
|
||||
path('counter/<int:pk>/', ArticleCounterRedirectView.as_view(), name='article-counter'),
|
||||
path('details/<int:pk>/', ArticleDetail.as_view(), name='article-detail'),
|
||||
path('go-to-django/', RedirectView.as_view(url='https://djangoproject.com'), name='go-to-django'),
|
||||
]
|
||||
|
||||
**Attributes**
|
||||
|
||||
@@ -63,15 +63,15 @@ views for displaying drilldown pages for date-based data.
|
||||
|
||||
**Example myapp/urls.py**::
|
||||
|
||||
from django.conf.urls import url
|
||||
from django.urls import path
|
||||
from django.views.generic.dates import ArchiveIndexView
|
||||
|
||||
from myapp.models import Article
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^archive/$',
|
||||
ArchiveIndexView.as_view(model=Article, date_field="pub_date"),
|
||||
name="article_archive"),
|
||||
path('archive/',
|
||||
ArchiveIndexView.as_view(model=Article, date_field="pub_date"),
|
||||
name="article_archive"),
|
||||
]
|
||||
|
||||
**Example myapp/article_archive.html**:
|
||||
@@ -162,14 +162,14 @@ views for displaying drilldown pages for date-based data.
|
||||
|
||||
**Example myapp/urls.py**::
|
||||
|
||||
from django.conf.urls import url
|
||||
from django.urls import path
|
||||
|
||||
from myapp.views import ArticleYearArchiveView
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^(?P<year>[0-9]{4})/$',
|
||||
ArticleYearArchiveView.as_view(),
|
||||
name="article_year_archive"),
|
||||
path('<int:year>/',
|
||||
ArticleYearArchiveView.as_view(),
|
||||
name="article_year_archive"),
|
||||
]
|
||||
|
||||
**Example myapp/article_archive_year.html**:
|
||||
@@ -254,19 +254,19 @@ views for displaying drilldown pages for date-based data.
|
||||
|
||||
**Example myapp/urls.py**::
|
||||
|
||||
from django.conf.urls import url
|
||||
from django.urls import path
|
||||
|
||||
from myapp.views import ArticleMonthArchiveView
|
||||
|
||||
urlpatterns = [
|
||||
# Example: /2012/aug/
|
||||
url(r'^(?P<year>[0-9]{4})/(?P<month>[-\w]+)/$',
|
||||
ArticleMonthArchiveView.as_view(),
|
||||
name="archive_month"),
|
||||
# Example: /2012/08/
|
||||
url(r'^(?P<year>[0-9]{4})/(?P<month>[0-9]+)/$',
|
||||
ArticleMonthArchiveView.as_view(month_format='%m'),
|
||||
name="archive_month_numeric"),
|
||||
path('<int:year>/<int:month>/',
|
||||
ArticleMonthArchiveView.as_view(month_format='%m'),
|
||||
name="archive_month_numeric"),
|
||||
# Example: /2012/aug/
|
||||
path('<int:year>/<str:month>/',
|
||||
ArticleMonthArchiveView.as_view(),
|
||||
name="archive_month"),
|
||||
]
|
||||
|
||||
**Example myapp/article_archive_month.html**:
|
||||
@@ -356,15 +356,15 @@ views for displaying drilldown pages for date-based data.
|
||||
|
||||
**Example myapp/urls.py**::
|
||||
|
||||
from django.conf.urls import url
|
||||
from django.urls import path
|
||||
|
||||
from myapp.views import ArticleWeekArchiveView
|
||||
|
||||
urlpatterns = [
|
||||
# Example: /2012/week/23/
|
||||
url(r'^(?P<year>[0-9]{4})/week/(?P<week>[0-9]+)/$',
|
||||
ArticleWeekArchiveView.as_view(),
|
||||
name="archive_week"),
|
||||
path('<int:year>/week/<int:week>/',
|
||||
ArticleWeekArchiveView.as_view(),
|
||||
name="archive_week"),
|
||||
]
|
||||
|
||||
**Example myapp/article_archive_week.html**:
|
||||
@@ -468,15 +468,15 @@ views for displaying drilldown pages for date-based data.
|
||||
|
||||
**Example myapp/urls.py**::
|
||||
|
||||
from django.conf.urls import url
|
||||
from django.urls import path
|
||||
|
||||
from myapp.views import ArticleDayArchiveView
|
||||
|
||||
urlpatterns = [
|
||||
# Example: /2012/nov/10/
|
||||
url(r'^(?P<year>[0-9]{4})/(?P<month>[-\w]+)/(?P<day>[0-9]+)/$',
|
||||
ArticleDayArchiveView.as_view(),
|
||||
name="archive_day"),
|
||||
path('<int:year>/<str:month>/<int:day>/',
|
||||
ArticleDayArchiveView.as_view(),
|
||||
name="archive_day"),
|
||||
]
|
||||
|
||||
**Example myapp/article_archive_day.html**:
|
||||
@@ -541,14 +541,14 @@ views for displaying drilldown pages for date-based data.
|
||||
|
||||
**Example myapp/urls.py**::
|
||||
|
||||
from django.conf.urls import url
|
||||
from django.urls import path
|
||||
|
||||
from myapp.views import ArticleTodayArchiveView
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^today/$',
|
||||
ArticleTodayArchiveView.as_view(),
|
||||
name="archive_today"),
|
||||
path('today/',
|
||||
ArticleTodayArchiveView.as_view(),
|
||||
name="archive_today"),
|
||||
]
|
||||
|
||||
.. admonition:: Where is the example template for ``TodayArchiveView``?
|
||||
@@ -591,13 +591,13 @@ views for displaying drilldown pages for date-based data.
|
||||
|
||||
**Example myapp/urls.py**::
|
||||
|
||||
from django.conf.urls import url
|
||||
from django.urls import path
|
||||
from django.views.generic.dates import DateDetailView
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^(?P<year>[0-9]{4})/(?P<month>[-\w]+)/(?P<day>[0-9]+)/(?P<pk>[0-9]+)/$',
|
||||
DateDetailView.as_view(model=Article, date_field="pub_date"),
|
||||
name="archive_date_detail"),
|
||||
path('<int:year>/<str:month>/<int:day>/<int:pk>/',
|
||||
DateDetailView.as_view(model=Article, date_field="pub_date"),
|
||||
name="archive_date_detail"),
|
||||
]
|
||||
|
||||
**Example myapp/article_detail.html**:
|
||||
|
||||
@@ -54,12 +54,12 @@ many projects they are typically the most commonly used views.
|
||||
|
||||
**Example myapp/urls.py**::
|
||||
|
||||
from django.conf.urls import url
|
||||
from django.urls import path
|
||||
|
||||
from article.views import ArticleDetailView
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^(?P<slug>[-\w]+)/$', ArticleDetailView.as_view(), name='article-detail'),
|
||||
path('<slug>/', ArticleDetailView.as_view(), name='article-detail'),
|
||||
]
|
||||
|
||||
**Example myapp/article_detail.html**:
|
||||
@@ -123,12 +123,12 @@ many projects they are typically the most commonly used views.
|
||||
|
||||
**Example myapp/urls.py**::
|
||||
|
||||
from django.conf.urls import url
|
||||
from django.urls import path
|
||||
|
||||
from article.views import ArticleListView
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^$', ArticleListView.as_view(), name='article-list'),
|
||||
path('', ArticleListView.as_view(), name='article-list'),
|
||||
]
|
||||
|
||||
**Example myapp/article_list.html**:
|
||||
|
||||
@@ -26,7 +26,7 @@ A class-based view is deployed into a URL pattern using the
|
||||
:meth:`~django.views.generic.base.View.as_view()` classmethod::
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^view/$', MyView.as_view(size=42)),
|
||||
path('view/', MyView.as_view(size=42)),
|
||||
]
|
||||
|
||||
.. admonition:: Thread safety with view arguments
|
||||
|
||||
@@ -15,7 +15,7 @@ Multiple object mixins
|
||||
* Use the ``page`` parameter in the URLconf. For example, this is what
|
||||
your URLconf might look like::
|
||||
|
||||
url(r'^objects/page(?P<page>[0-9]+)/$', PaginatedView.as_view()),
|
||||
path('objects/page<int:page>/', PaginatedView.as_view()),
|
||||
|
||||
* Pass the page number via the ``page`` query-string parameter. For
|
||||
example, a URL would look like this::
|
||||
|
||||
Reference in New Issue
Block a user