mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #22218 -- Deprecated django.conf.urls.patterns.
Thanks Carl Meyer for the suggestion and Alex Gaynor and Carl for reviews.
This commit is contained in:
@@ -119,12 +119,12 @@ Now we need to define a view::
|
||||
Finally hook that view into your urls::
|
||||
|
||||
# urls.py
|
||||
from django.conf.urls import patterns, url
|
||||
from django.conf.urls import url
|
||||
from books.views import PublisherList
|
||||
|
||||
urlpatterns = patterns('',
|
||||
urlpatterns = [
|
||||
url(r'^publishers/$', PublisherList.as_view()),
|
||||
)
|
||||
]
|
||||
|
||||
That's all the Python code we need to write. We still need to write a template,
|
||||
however. We could explicitly tell the view which template to use by adding a
|
||||
@@ -330,12 +330,12 @@ 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 patterns
|
||||
from django.conf.urls import url
|
||||
from books.views import PublisherBookList
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r'^books/([\w-]+)/$', PublisherBookList.as_view()),
|
||||
)
|
||||
urlpatterns = [
|
||||
url(r'^books/([\w-]+)/$', PublisherBookList.as_view()),
|
||||
]
|
||||
|
||||
Next, we'll write the ``PublisherBookList`` view itself::
|
||||
|
||||
@@ -396,13 +396,13 @@ 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 patterns, url
|
||||
from django.conf.urls import url
|
||||
from books.views import AuthorDetailView
|
||||
|
||||
urlpatterns = patterns('',
|
||||
urlpatterns = [
|
||||
#...
|
||||
url(r'^authors/(?P<pk>\d+)/$', AuthorDetailView.as_view(), name='author-detail'),
|
||||
)
|
||||
]
|
||||
|
||||
Then we'd write our new view -- ``get_object`` is the method that retrieves the
|
||||
object -- so we simply override it and wrap the call::
|
||||
|
||||
@@ -141,15 +141,15 @@ an :exc:`~django.core.exceptions.ImproperlyConfigured` exception if it's not.
|
||||
Finally, we hook these new views into the URLconf::
|
||||
|
||||
# urls.py
|
||||
from django.conf.urls import patterns, url
|
||||
from django.conf.urls import url
|
||||
from myapp.views import AuthorCreate, AuthorUpdate, AuthorDelete
|
||||
|
||||
urlpatterns = patterns('',
|
||||
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'),
|
||||
)
|
||||
]
|
||||
|
||||
.. note::
|
||||
|
||||
|
||||
@@ -38,12 +38,12 @@ 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 patterns
|
||||
from django.conf.urls import url
|
||||
from django.views.generic import TemplateView
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r'^about/', TemplateView.as_view(template_name="about.html")),
|
||||
)
|
||||
urlpatterns = [
|
||||
url(r'^about/', TemplateView.as_view(template_name="about.html")),
|
||||
]
|
||||
|
||||
Any arguments passed to :meth:`~django.views.generic.base.View.as_view` will
|
||||
override attributes set on the class. In this example, we set ``template_name``
|
||||
@@ -75,12 +75,12 @@ class method instead, which provides a function-like entry to class-based
|
||||
views::
|
||||
|
||||
# urls.py
|
||||
from django.conf.urls import patterns
|
||||
from django.conf.urls import url
|
||||
from some_app.views import AboutView
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r'^about/', AboutView.as_view()),
|
||||
)
|
||||
urlpatterns = [
|
||||
url(r'^about/', AboutView.as_view()),
|
||||
]
|
||||
|
||||
|
||||
For more information on how to use the built in generic views, consult the next
|
||||
@@ -100,12 +100,12 @@ 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 patterns
|
||||
from django.conf.urls import url
|
||||
from books.views import BookListView
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r'^books/$', BookListView.as_view()),
|
||||
)
|
||||
urlpatterns = [
|
||||
url(r'^books/$', BookListView.as_view()),
|
||||
]
|
||||
|
||||
And the view::
|
||||
|
||||
|
||||
@@ -89,12 +89,12 @@ request to a matching method if one is defined, or raises
|
||||
:class:`~django.http.HttpResponseNotAllowed` if not::
|
||||
|
||||
# urls.py
|
||||
from django.conf.urls import patterns
|
||||
from django.conf.urls import url
|
||||
from myapp.views import MyView
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r'^about/', MyView.as_view()),
|
||||
)
|
||||
urlpatterns = [
|
||||
url(r'^about/', MyView.as_view()),
|
||||
]
|
||||
|
||||
|
||||
It is worth noting that what your method returns is identical to what you
|
||||
@@ -129,9 +129,9 @@ You can override that in a subclass::
|
||||
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 = patterns('',
|
||||
(r'^about/', GreetingView.as_view(greeting="G'day")),
|
||||
)
|
||||
urlpatterns = [
|
||||
url(r'^about/', GreetingView.as_view(greeting="G'day")),
|
||||
]
|
||||
|
||||
.. note::
|
||||
|
||||
@@ -268,10 +268,10 @@ The easiest place to do this is in the URLconf where you deploy your view::
|
||||
|
||||
from .views import VoteView
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r'^about/', login_required(TemplateView.as_view(template_name="secret.html"))),
|
||||
(r'^vote/', permission_required('polls.can_vote')(VoteView.as_view())),
|
||||
)
|
||||
urlpatterns = [
|
||||
url(r'^about/', login_required(TemplateView.as_view(template_name="secret.html"))),
|
||||
url(r'^vote/', permission_required('polls.can_vote')(VoteView.as_view())),
|
||||
]
|
||||
|
||||
This approach applies the decorator on a per-instance basis. If you
|
||||
want every instance of a view to be decorated, you need to take a
|
||||
|
||||
@@ -256,13 +256,13 @@ mixin.
|
||||
We can hook this into our URLs easily enough::
|
||||
|
||||
# urls.py
|
||||
from django.conf.urls import patterns, url
|
||||
from django.conf.urls import url
|
||||
from books.views import RecordInterest
|
||||
|
||||
urlpatterns = patterns('',
|
||||
urlpatterns = [
|
||||
#...
|
||||
url(r'^author/(?P<pk>\d+)/interest/$', RecordInterest.as_view(), name='author-interest'),
|
||||
)
|
||||
]
|
||||
|
||||
Note the ``pk`` named group, which
|
||||
:meth:`~django.views.generic.detail.SingleObjectMixin.get_object` uses
|
||||
|
||||
Reference in New Issue
Block a user