diff options
| author | Sjoerd Job Postmus <sjoerdjob@sjec.nl> | 2016-10-20 19:29:04 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2017-09-20 18:04:42 -0400 |
| commit | df41b5a05d4e00e80e73afe629072e37873e767a (patch) | |
| tree | baaf71ae695e2d3af604ea0d663284cb406c71e4 /docs/topics/class-based-views | |
| parent | c4c128d67c7dc2830631c6859a204c9d259f1fb1 (diff) | |
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.
Diffstat (limited to 'docs/topics/class-based-views')
| -rw-r--r-- | docs/topics/class-based-views/generic-display.txt | 14 | ||||
| -rw-r--r-- | docs/topics/class-based-views/generic-editing.txt | 8 | ||||
| -rw-r--r-- | docs/topics/class-based-views/index.txt | 12 | ||||
| -rw-r--r-- | docs/topics/class-based-views/intro.txt | 10 | ||||
| -rw-r--r-- | docs/topics/class-based-views/mixins.txt | 4 |
5 files changed, 24 insertions, 24 deletions
diff --git a/docs/topics/class-based-views/generic-display.txt b/docs/topics/class-based-views/generic-display.txt index 456c953cb1..2f41b0b500 100644 --- a/docs/topics/class-based-views/generic-display.txt +++ b/docs/topics/class-based-views/generic-display.txt @@ -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 diff --git a/docs/topics/class-based-views/generic-editing.txt b/docs/topics/class-based-views/generic-editing.txt index 3fde51253a..69f2158021 100644 --- a/docs/topics/class-based-views/generic-editing.txt +++ b/docs/topics/class-based-views/generic-editing.txt @@ -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:: diff --git a/docs/topics/class-based-views/index.txt b/docs/topics/class-based-views/index.txt index 91dd6ff944..6a629cb887 100644 --- a/docs/topics/class-based-views/index.txt +++ b/docs/topics/class-based-views/index.txt @@ -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:: diff --git a/docs/topics/class-based-views/intro.txt b/docs/topics/class-based-views/intro.txt index 32562a8744..f4a5f5ac24 100644 --- a/docs/topics/class-based-views/intro.txt +++ b/docs/topics/class-based-views/intro.txt @@ -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 diff --git a/docs/topics/class-based-views/mixins.txt b/docs/topics/class-based-views/mixins.txt index 8da0834866..4a47af6f11 100644 --- a/docs/topics/class-based-views/mixins.txt +++ b/docs/topics/class-based-views/mixins.txt @@ -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 |
