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:
Sjoerd Job Postmus 2016-10-20 19:29:04 +02:00 committed by Tim Graham
parent c4c128d67c
commit df41b5a05d
77 changed files with 1663 additions and 1105 deletions

View file

@ -117,11 +117,11 @@ Now we need to define a view::
Finally hook that view into your urls::
# urls.py
from django.conf.urls import url
from django.urls import path
from books.views import PublisherList
urlpatterns = [
url(r'^publishers/$', PublisherList.as_view()),
path('publishers/', PublisherList.as_view()),
]
That's all the Python code we need to write. We still need to write a template,
@ -332,11 +332,11 @@ various useful things are stored on ``self``; as well as the request
Here, we have a URLconf with a single captured group::
# urls.py
from django.conf.urls import url
from django.urls import path
from books.views import PublisherBookList
urlpatterns = [
url(r'^books/([\w-]+)/$', PublisherBookList.as_view()),
path('books/<publisher>/', PublisherBookList.as_view()),
]
Next, we'll write the ``PublisherBookList`` view itself::
@ -351,7 +351,7 @@ Next, we'll write the ``PublisherBookList`` view itself::
template_name = 'books/books_by_publisher.html'
def get_queryset(self):
self.publisher = get_object_or_404(Publisher, name=self.args[0])
self.publisher = get_object_or_404(Publisher, name=self.kwargs['publisher'])
return Book.objects.filter(publisher=self.publisher)
As you can see, it's quite easy to add more logic to the queryset selection;
@ -398,12 +398,12 @@ updated.
First, we'd need to add an author detail bit in the URLconf to point to a
custom view::
from django.conf.urls import url
from django.urls import path
from books.views import AuthorDetailView
urlpatterns = [
#...
url(r'^authors/(?P<pk>[0-9]+)/$', AuthorDetailView.as_view(), name='author-detail'),
path('authors/<int:pk>/', AuthorDetailView.as_view(), name='author-detail'),
]
Then we'd write our new view -- ``get_object`` is the method that retrieves the

View file

@ -149,14 +149,14 @@ Finally, we hook these new views into the URLconf:
.. snippet::
:filename: urls.py
from django.conf.urls import url
from django.urls import path
from myapp.views import AuthorCreate, AuthorUpdate, AuthorDelete
urlpatterns = [
# ...
url(r'author/add/$', AuthorCreate.as_view(), name='author-add'),
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'),
path('author/add/', AuthorCreate.as_view(), name='author-add'),
path('author/<int:pk>/', AuthorUpdate.as_view(), name='author-update'),
path('author/<int:pk>/delete/', AuthorDelete.as_view(), name='author-delete'),
]
.. note::

View file

@ -38,11 +38,11 @@ URLconf. If you're only changing a few simple attributes on a class-based view,
you can simply pass them into the
:meth:`~django.views.generic.base.View.as_view` method call itself::
from django.conf.urls import url
from django.urls import path
from django.views.generic import TemplateView
urlpatterns = [
url(r'^about/$', TemplateView.as_view(template_name="about.html")),
path('about/', TemplateView.as_view(template_name="about.html")),
]
Any arguments passed to :meth:`~django.views.generic.base.View.as_view` will
@ -75,11 +75,11 @@ class method instead, which provides a function-like entry to class-based
views::
# urls.py
from django.conf.urls import url
from django.urls import path
from some_app.views import AboutView
urlpatterns = [
url(r'^about/$', AboutView.as_view()),
path('about/', AboutView.as_view()),
]
@ -100,11 +100,11 @@ preferable to ask the API when the most recent book was published.
We map the URL to book list view in the URLconf::
from django.conf.urls import url
from django.urls import path
from books.views import BookListView
urlpatterns = [
url(r'^books/$', BookListView.as_view()),
path('books/', BookListView.as_view()),
]
And the view::

View file

@ -89,11 +89,11 @@ request to a matching method if one is defined, or raises
:class:`~django.http.HttpResponseNotAllowed` if not::
# urls.py
from django.conf.urls import url
from django.urls import path
from myapp.views import MyView
urlpatterns = [
url(r'^about/$', MyView.as_view()),
path('about/', MyView.as_view()),
]
@ -130,7 +130,7 @@ Another option is to configure class attributes as keyword arguments to the
:meth:`~django.views.generic.base.View.as_view` call in the URLconf::
urlpatterns = [
url(r'^about/$', GreetingView.as_view(greeting="G'day")),
path('about/', GreetingView.as_view(greeting="G'day")),
]
.. note::
@ -245,8 +245,8 @@ The easiest place to do this is in the URLconf where you deploy your view::
from .views import VoteView
urlpatterns = [
url(r'^about/$', login_required(TemplateView.as_view(template_name="secret.html"))),
url(r'^vote/$', permission_required('polls.can_vote')(VoteView.as_view())),
path('about/', login_required(TemplateView.as_view(template_name="secret.html"))),
path('vote/', permission_required('polls.can_vote')(VoteView.as_view())),
]
This approach applies the decorator on a per-instance basis. If you

View file

@ -258,12 +258,12 @@ We can hook this into our URLs easily enough:
.. snippet::
:filename: urls.py
from django.conf.urls import url
from django.urls import path
from books.views import RecordInterest
urlpatterns = [
#...
url(r'^author/(?P<pk>[0-9]+)/interest/$', RecordInterest.as_view(), name='author-interest'),
path('author/<int:pk>/interest/', RecordInterest.as_view(), name='author-interest'),
]
Note the ``pk`` named group, which