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:
@@ -532,9 +532,9 @@ The per-view cache, like the per-site cache, is keyed off of the URL. If
|
||||
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 = ('',
|
||||
(r'^foo/(\d{1,2})/$', my_view),
|
||||
)
|
||||
urlpatterns = [
|
||||
url(r'^foo/(\d{1,2})/$', my_view),
|
||||
]
|
||||
|
||||
then requests to ``/foo/1/`` and ``/foo/23/`` will be cached separately, as
|
||||
you may expect. But once a particular URL (e.g., ``/foo/23/``) has been
|
||||
@@ -578,17 +578,17 @@ themselves.
|
||||
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 = ('',
|
||||
(r'^foo/(\d{1,2})/$', my_view),
|
||||
)
|
||||
urlpatterns = [
|
||||
url(r'^foo/(\d{1,2})/$', my_view),
|
||||
]
|
||||
|
||||
Here's the same thing, with ``my_view`` wrapped in ``cache_page``::
|
||||
|
||||
from django.views.decorators.cache import cache_page
|
||||
|
||||
urlpatterns = ('',
|
||||
(r'^foo/(\d{1,2})/$', cache_page(60 * 15)(my_view)),
|
||||
)
|
||||
urlpatterns = [
|
||||
url(r'^foo/(\d{1,2})/$', cache_page(60 * 15)(my_view)),
|
||||
]
|
||||
|
||||
.. templatetag:: cache
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -45,8 +45,8 @@ algorithm the system follows to determine which Python code to execute:
|
||||
will be used in place of the :setting:`ROOT_URLCONF` setting.
|
||||
|
||||
2. Django loads that Python module and looks for the variable
|
||||
``urlpatterns``. This should be a Python list, in the format returned by
|
||||
the function :func:`django.conf.urls.patterns`.
|
||||
``urlpatterns``. This should be a Python list of :func:`django.conf.urls.url`
|
||||
instances.
|
||||
|
||||
3. Django runs through each URL pattern, in order, and stops at the first
|
||||
one that matches the requested URL.
|
||||
@@ -72,14 +72,14 @@ Example
|
||||
|
||||
Here's a sample URLconf::
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
from django.conf.urls import url
|
||||
|
||||
urlpatterns = patterns('',
|
||||
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'),
|
||||
)
|
||||
]
|
||||
|
||||
Notes:
|
||||
|
||||
@@ -129,14 +129,14 @@ is ``(?P<name>pattern)``, where ``name`` is the name of the group and
|
||||
|
||||
Here's the above example URLconf, rewritten to use named groups::
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
from django.conf.urls import url
|
||||
|
||||
urlpatterns = patterns('',
|
||||
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'),
|
||||
)
|
||||
]
|
||||
|
||||
This accomplishes exactly the same thing as the previous example, with one
|
||||
subtle difference: The captured values are passed to view functions as keyword
|
||||
@@ -203,12 +203,12 @@ A convenient trick is to specify default parameters for your views' arguments.
|
||||
Here's an example URLconf and view::
|
||||
|
||||
# URLconf
|
||||
from django.conf.urls import patterns, url
|
||||
from django.conf.urls import url
|
||||
|
||||
urlpatterns = patterns('',
|
||||
urlpatterns = [
|
||||
url(r'^blog/$', 'blog.views.page'),
|
||||
url(r'^blog/page(?P<num>\d+)/$', 'blog.views.page'),
|
||||
)
|
||||
]
|
||||
|
||||
# View (in blog/views.py)
|
||||
def page(request, num="1"):
|
||||
@@ -230,9 +230,8 @@ accessed. This makes the system blazingly fast.
|
||||
Syntax of the urlpatterns variable
|
||||
==================================
|
||||
|
||||
``urlpatterns`` should be a Python list, in the format returned by the function
|
||||
:func:`django.conf.urls.patterns`. Always use ``patterns()`` to create
|
||||
the ``urlpatterns`` variable.
|
||||
``urlpatterns`` should be a Python list of :func:`~django.conf.urls.url`
|
||||
instances.
|
||||
|
||||
Error handling
|
||||
==============
|
||||
@@ -260,73 +259,6 @@ The variables are:
|
||||
* ``handler403`` -- See :data:`django.conf.urls.handler403`.
|
||||
* ``handler400`` -- See :data:`django.conf.urls.handler400`.
|
||||
|
||||
.. _urlpatterns-view-prefix:
|
||||
|
||||
The view prefix
|
||||
===============
|
||||
|
||||
You can specify a common prefix in your ``patterns()`` call, to cut down on
|
||||
code duplication.
|
||||
|
||||
Here's the example 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'),
|
||||
)
|
||||
|
||||
In this example, each view has a common prefix -- ``'news.views'``.
|
||||
Instead of typing that out for each entry in ``urlpatterns``, you can use the
|
||||
first argument to the ``patterns()`` function to specify a prefix to apply to
|
||||
each view function.
|
||||
|
||||
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'),
|
||||
)
|
||||
|
||||
Note that you don't put a trailing dot (``"."``) in the prefix. Django puts
|
||||
that in automatically.
|
||||
|
||||
Multiple view prefixes
|
||||
----------------------
|
||||
|
||||
In practice, you'll probably end up mixing and matching views to the point
|
||||
where the views in your ``urlpatterns`` won't have a common prefix. However,
|
||||
you can still take advantage of the view prefix shortcut to remove duplication.
|
||||
Just add multiple ``patterns()`` objects together, like this:
|
||||
|
||||
Old::
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^$', 'myapp.views.app_index'),
|
||||
url(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$', 'myapp.views.month_display'),
|
||||
url(r'^tag/(?P<tag>\w+)/$', 'weblog.views.tag'),
|
||||
)
|
||||
|
||||
New::
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
|
||||
urlpatterns = patterns('myapp.views',
|
||||
url(r'^$', 'app_index'),
|
||||
url(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$','month_display'),
|
||||
)
|
||||
|
||||
urlpatterns += patterns('weblog.views',
|
||||
url(r'^tag/(?P<tag>\w+)/$', 'tag'),
|
||||
)
|
||||
|
||||
.. _including-other-urlconfs:
|
||||
|
||||
Including other URLconfs
|
||||
@@ -338,14 +270,14 @@ essentially "roots" a set of URLs below other ones.
|
||||
For example, here's an excerpt of the URLconf for the `Django Web site`_
|
||||
itself. It includes a number of other URLconfs::
|
||||
|
||||
from django.conf.urls import include, patterns, url
|
||||
from django.conf.urls import include, url
|
||||
|
||||
urlpatterns = patterns('',
|
||||
urlpatterns = [
|
||||
# ... snip ...
|
||||
url(r'^community/', include('django_website.aggregator.urls')),
|
||||
url(r'^contact/', include('django_website.contact.urls')),
|
||||
# ... snip ...
|
||||
)
|
||||
]
|
||||
|
||||
Note that the regular expressions in this example don't have a ``$``
|
||||
(end-of-string match character) but do include a trailing slash. Whenever
|
||||
@@ -353,23 +285,21 @@ Django encounters ``include()`` (:func:`django.conf.urls.include()`), it chops
|
||||
off whatever part of the URL matched up to that point and sends the remaining
|
||||
string to the included URLconf for further processing.
|
||||
|
||||
Another possibility is to include additional URL patterns not by specifying the
|
||||
URLconf Python module defining them as the ``include()`` argument but by using
|
||||
directly the pattern list as returned by :func:`~django.conf.urls.patterns`
|
||||
instead. For example, consider this URLconf::
|
||||
Another possibility is to include additional URL patterns by using a list of
|
||||
:func:`~django.conf.urls.url` instances. For example, consider this URLconf::
|
||||
|
||||
from django.conf.urls import include, patterns, url
|
||||
from django.conf.urls import include, url
|
||||
|
||||
extra_patterns = patterns('',
|
||||
extra_patterns = [
|
||||
url(r'^reports/(?P<id>\d+)/$', 'credit.views.report'),
|
||||
url(r'^charge/$', 'credit.views.charge'),
|
||||
)
|
||||
]
|
||||
|
||||
urlpatterns = patterns('',
|
||||
urlpatterns = [
|
||||
url(r'^$', 'apps.main.views.homepage'),
|
||||
url(r'^help/', include('apps.help.urls')),
|
||||
url(r'^credit/', include(extra_patterns)),
|
||||
)
|
||||
]
|
||||
|
||||
In this example, the ``/credit/reports/`` URL will be handled by the
|
||||
``credit.views.report()`` Django view.
|
||||
@@ -377,28 +307,30 @@ In this example, the ``/credit/reports/`` URL will be handled by the
|
||||
This can be used to remove redundancy from URLconfs where a single pattern
|
||||
prefix is used repeatedly. For example, consider this URLconf::
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
from django.conf.urls import url
|
||||
from . import views
|
||||
|
||||
urlpatterns = patterns('wiki.views',
|
||||
url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/history/$', 'history'),
|
||||
url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/edit/$', 'edit'),
|
||||
url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/discuss/$', 'discuss'),
|
||||
url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/permissions/$', 'permissions'),
|
||||
)
|
||||
urlpatterns = [
|
||||
url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/history/$', views.history),
|
||||
url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/edit/$', views.edit),
|
||||
url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/discuss/$', views.discuss),
|
||||
url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/permissions/$', views.permissions),
|
||||
]
|
||||
|
||||
We can improve this by stating the common path prefix only once and grouping
|
||||
the suffixes that differ::
|
||||
|
||||
from django.conf.urls import include, patterns, url
|
||||
from django.conf.urls import include, url
|
||||
from . import views
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/', include(patterns('wiki.views',
|
||||
url(r'^history/$', 'history'),
|
||||
url(r'^edit/$', 'edit'),
|
||||
url(r'^discuss/$', 'discuss'),
|
||||
url(r'^permissions/$', 'permissions'),
|
||||
))),
|
||||
)
|
||||
urlpatterns = [
|
||||
url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/', include([
|
||||
url(r'^history/$', views.history),
|
||||
url(r'^edit/$', views.edit),
|
||||
url(r'^discuss/$', views.discuss),
|
||||
url(r'^permissions/$', views.permissions),
|
||||
])),
|
||||
]
|
||||
|
||||
.. _`Django Web site`: https://www.djangoproject.com/
|
||||
|
||||
@@ -409,19 +341,20 @@ An included URLconf receives any captured parameters from parent URLconfs, so
|
||||
the following example is valid::
|
||||
|
||||
# In settings/urls/main.py
|
||||
from django.conf.urls import include, patterns, url
|
||||
from django.conf.urls import include, url
|
||||
|
||||
urlpatterns = patterns('',
|
||||
urlpatterns = [
|
||||
url(r'^(?P<username>\w+)/blog/', include('foo.urls.blog')),
|
||||
)
|
||||
]
|
||||
|
||||
# In foo/urls/blog.py
|
||||
from django.conf.urls import patterns, url
|
||||
from django.conf.urls import url
|
||||
from . import views
|
||||
|
||||
urlpatterns = patterns('foo.views',
|
||||
url(r'^$', 'blog.index'),
|
||||
url(r'^archive/$', 'blog.archive'),
|
||||
)
|
||||
urlpatterns = [
|
||||
url(r'^$', views.blog.index),
|
||||
url(r'^archive/$', views.blog.archive),
|
||||
]
|
||||
|
||||
In the above example, the captured ``"username"`` variable is passed to the
|
||||
included URLconf, as expected.
|
||||
@@ -440,11 +373,12 @@ function.
|
||||
|
||||
For example::
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
from django.conf.urls import url
|
||||
from . import views
|
||||
|
||||
urlpatterns = patterns('blog.views',
|
||||
url(r'^blog/(?P<year>\d{4})/$', 'year_archive', {'foo': 'bar'}),
|
||||
)
|
||||
urlpatterns = [
|
||||
url(r'^blog/(?P<year>\d{4})/$', views.year_archive, {'foo': 'bar'}),
|
||||
]
|
||||
|
||||
In this example, for a request to ``/blog/2005/``, Django will call
|
||||
``blog.views.year_archive(request, year='2005', foo='bar')``.
|
||||
@@ -472,36 +406,38 @@ For example, these two URLconf sets are functionally identical:
|
||||
Set one::
|
||||
|
||||
# main.py
|
||||
from django.conf.urls import include, patterns, url
|
||||
from django.conf.urls import include, url
|
||||
|
||||
urlpatterns = patterns('',
|
||||
urlpatterns = [
|
||||
url(r'^blog/', include('inner'), {'blogid': 3}),
|
||||
)
|
||||
]
|
||||
|
||||
# inner.py
|
||||
from django.conf.urls import patterns, url
|
||||
from django.conf.urls import url
|
||||
from mysite import views
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^archive/$', 'mysite.views.archive'),
|
||||
url(r'^about/$', 'mysite.views.about'),
|
||||
)
|
||||
urlpatterns = [
|
||||
url(r'^archive/$', views.archive),
|
||||
url(r'^about/$', views.about),
|
||||
]
|
||||
|
||||
Set two::
|
||||
|
||||
# main.py
|
||||
from django.conf.urls import include, patterns, url
|
||||
from django.conf.urls import include, url
|
||||
from mysite import views
|
||||
|
||||
urlpatterns = patterns('',
|
||||
urlpatterns = [
|
||||
url(r'^blog/', include('inner')),
|
||||
)
|
||||
]
|
||||
|
||||
# inner.py
|
||||
from django.conf.urls import patterns, url
|
||||
from django.conf.urls import url
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^archive/$', 'mysite.views.archive', {'blogid': 3}),
|
||||
url(r'^about/$', 'mysite.views.about', {'blogid': 3}),
|
||||
)
|
||||
urlpatterns = [
|
||||
url(r'^archive/$', views.archive, {'blogid': 3}),
|
||||
url(r'^about/$', views.about, {'blogid': 3}),
|
||||
]
|
||||
|
||||
Note that extra options will *always* be passed to *every* line in the included
|
||||
URLconf, regardless of whether the line's view actually accepts those options
|
||||
@@ -517,38 +453,38 @@ supported -- you can pass any callable object as the view.
|
||||
|
||||
For example, given this URLconf in "string" notation::
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
from django.conf.urls import url
|
||||
|
||||
urlpatterns = patterns('',
|
||||
urlpatterns = [
|
||||
url(r'^archive/$', 'mysite.views.archive'),
|
||||
url(r'^about/$', 'mysite.views.about'),
|
||||
url(r'^contact/$', 'mysite.views.contact'),
|
||||
)
|
||||
]
|
||||
|
||||
You can accomplish the same thing by passing objects rather than strings. Just
|
||||
be sure to import the objects::
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
from django.conf.urls import url
|
||||
from mysite.views import archive, about, contact
|
||||
|
||||
urlpatterns = patterns('',
|
||||
urlpatterns = [
|
||||
url(r'^archive/$', archive),
|
||||
url(r'^about/$', about),
|
||||
url(r'^contact/$', contact),
|
||||
)
|
||||
]
|
||||
|
||||
The following example is functionally identical. It's just a bit more compact
|
||||
because it imports the module that contains the views, rather than importing
|
||||
each view individually::
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
from django.conf.urls import url
|
||||
from mysite import views
|
||||
|
||||
urlpatterns = patterns('',
|
||||
urlpatterns = [
|
||||
url(r'^archive/$', views.archive),
|
||||
url(r'^about/$', views.about),
|
||||
url(r'^contact/$', views.contact),
|
||||
)
|
||||
]
|
||||
|
||||
The style you use is up to you.
|
||||
|
||||
@@ -558,12 +494,12 @@ the view prefix (as explained in "The view prefix" above) will have no effect.
|
||||
Note that :doc:`class based views</topics/class-based-views/index>` must be
|
||||
imported::
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
from django.conf.urls import url
|
||||
from mysite.views import ClassBasedView
|
||||
|
||||
urlpatterns = patterns('',
|
||||
urlpatterns = [
|
||||
url(r'^myview/$', ClassBasedView.as_view()),
|
||||
)
|
||||
]
|
||||
|
||||
Reverse resolution of URLs
|
||||
==========================
|
||||
@@ -618,13 +554,13 @@ Examples
|
||||
|
||||
Consider again this URLconf entry::
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
from django.conf.urls import url
|
||||
|
||||
urlpatterns = patterns('',
|
||||
urlpatterns = [
|
||||
#...
|
||||
url(r'^articles/(\d{4})/$', 'news.views.year_archive'),
|
||||
#...
|
||||
)
|
||||
]
|
||||
|
||||
According to this design, the URL for the archive corresponding to year *nnnn*
|
||||
is ``/articles/nnnn/``.
|
||||
@@ -670,13 +606,13 @@ It's fairly common to use the same view function in multiple URL patterns in
|
||||
your URLconf. For example, these two URL patterns both point to the ``archive``
|
||||
view::
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
from django.conf.urls import url
|
||||
from mysite.views import archive
|
||||
|
||||
urlpatterns = patterns('',
|
||||
urlpatterns = [
|
||||
url(r'^archive/(\d{4})/$', archive),
|
||||
url(r'^archive-summary/(\d{4})/$', archive, {'summary': True}),
|
||||
)
|
||||
]
|
||||
|
||||
This is completely valid, but it leads to problems when you try to do reverse
|
||||
URL matching (through the :func:`~django.core.urlresolvers.reverse` function
|
||||
@@ -691,13 +627,13 @@ matching.
|
||||
|
||||
Here's the above example, rewritten to use named URL patterns::
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
from django.conf.urls import url
|
||||
from mysite.views import archive
|
||||
|
||||
urlpatterns = patterns('',
|
||||
urlpatterns = [
|
||||
url(r'^archive/(\d{4})/$', archive, name="full-archive"),
|
||||
url(r'^archive-summary/(\d{4})/$', archive, {'summary': True}, name="arch-summary"),
|
||||
)
|
||||
]
|
||||
|
||||
With these names in place (``full-archive`` and ``arch-summary``), you can
|
||||
target each pattern individually by using its name:
|
||||
@@ -859,20 +795,20 @@ This will include the URLs defined in ``apps.help.urls`` into the
|
||||
``'foo'``.
|
||||
|
||||
Secondly, you can include an object that contains embedded namespace data. If
|
||||
you ``include()`` an object as returned by :func:`~django.conf.urls.patterns`,
|
||||
you ``include()`` a list of :func:`django.conf.urls.url` instances,
|
||||
the URLs contained in that object will be added to the global namespace.
|
||||
However, you can also ``include()`` a 3-tuple containing::
|
||||
|
||||
(<patterns object>, <application namespace>, <instance namespace>)
|
||||
(<list of url() instances>, <application namespace>, <instance namespace>)
|
||||
|
||||
For example::
|
||||
|
||||
from django.conf.urls import include, patterns, url
|
||||
from django.conf.urls import include, url
|
||||
|
||||
help_patterns = patterns('',
|
||||
help_patterns = [
|
||||
url(r'^basic/$', 'apps.help.views.views.basic'),
|
||||
url(r'^advanced/$', 'apps.help.views.views.advanced'),
|
||||
)
|
||||
]
|
||||
|
||||
url(r'^help/', include((help_patterns, 'bar', 'foo'))),
|
||||
|
||||
|
||||
@@ -911,13 +911,15 @@ URL. Paths listed in :setting:`LOCALE_PATHS` are also included.
|
||||
|
||||
You hook it up like this::
|
||||
|
||||
from django.views.i18n import javascript_catalog
|
||||
|
||||
js_info_dict = {
|
||||
'packages': ('your.app.package',),
|
||||
}
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r'^jsi18n/$', 'django.views.i18n.javascript_catalog', js_info_dict),
|
||||
)
|
||||
urlpatterns = [
|
||||
url(r'^jsi18n/$', javascript_catalog, js_info_dict),
|
||||
]
|
||||
|
||||
Each string in ``packages`` should be in Python dotted-package syntax (the
|
||||
same format as the strings in :setting:`INSTALLED_APPS`) and should refer to a
|
||||
@@ -935,9 +937,9 @@ changed by altering the ``domain`` argument.
|
||||
|
||||
You can make the view dynamic by putting the packages into the URL pattern::
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r'^jsi18n/(?P<packages>\S+?)/$', 'django.views.i18n.javascript_catalog'),
|
||||
)
|
||||
urlpatterns = [
|
||||
url(r'^jsi18n/(?P<packages>\S+?)/$', javascript_catalog),
|
||||
]
|
||||
|
||||
With this, you specify the packages as a list of package names delimited by '+'
|
||||
signs in the URL. This is especially useful if your pages use code from
|
||||
@@ -1085,25 +1087,30 @@ Language prefix in URL patterns
|
||||
|
||||
.. function:: i18n_patterns(prefix, pattern_description, ...)
|
||||
|
||||
This function can be used in your root URLconf as a replacement for the normal
|
||||
:func:`django.conf.urls.patterns` function. Django will automatically
|
||||
.. deprecated:: 1.8
|
||||
|
||||
The ``prefix`` argument to ``i18n_patterns()`` has been deprecated and will
|
||||
not be supported in Django 2.0. Simply pass a list of
|
||||
:func:`django.conf.urls.url` instances instead.
|
||||
|
||||
This function can be used in your root URLconf and Django will automatically
|
||||
prepend the current active language code to all url patterns defined within
|
||||
:func:`~django.conf.urls.i18n.i18n_patterns`. Example URL patterns::
|
||||
|
||||
from django.conf.urls import patterns, include, url
|
||||
from django.conf.urls import include, url
|
||||
from django.conf.urls.i18n import i18n_patterns
|
||||
|
||||
urlpatterns = patterns('',
|
||||
urlpatterns = [
|
||||
url(r'^sitemap\.xml$', 'sitemap.view', name='sitemap_xml'),
|
||||
)
|
||||
]
|
||||
|
||||
news_patterns = patterns('',
|
||||
news_patterns = [
|
||||
url(r'^$', 'news.views.index', name='index'),
|
||||
url(r'^category/(?P<slug>[\w-]+)/$', 'news.views.category', name='category'),
|
||||
url(r'^(?P<slug>[\w-]+)/$', 'news.views.details', name='detail'),
|
||||
)
|
||||
]
|
||||
|
||||
urlpatterns += i18n_patterns('',
|
||||
urlpatterns += i18n_patterns(
|
||||
url(r'^about/$', 'about.view', name='about'),
|
||||
url(r'^news/', include(news_patterns, namespace='news')),
|
||||
)
|
||||
@@ -1144,21 +1151,21 @@ Translating URL patterns
|
||||
URL patterns can also be marked translatable using the
|
||||
:func:`~django.utils.translation.ugettext_lazy` function. Example::
|
||||
|
||||
from django.conf.urls import patterns, include, url
|
||||
from django.conf.urls import include, url
|
||||
from django.conf.urls.i18n import i18n_patterns
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
urlpatterns = patterns(''
|
||||
urlpatterns = [
|
||||
url(r'^sitemap\.xml$', 'sitemap.view', name='sitemap_xml'),
|
||||
)
|
||||
]
|
||||
|
||||
news_patterns = patterns(''
|
||||
news_patterns = [
|
||||
url(r'^$', 'news.views.index', name='index'),
|
||||
url(_(r'^category/(?P<slug>[\w-]+)/$'), 'news.views.category', name='category'),
|
||||
url(r'^(?P<slug>[\w-]+)/$', 'news.views.details', name='detail'),
|
||||
)
|
||||
]
|
||||
|
||||
urlpatterns += i18n_patterns('',
|
||||
urlpatterns += i18n_patterns(
|
||||
url(_(r'^about/$'), 'about.view', name='about'),
|
||||
url(_(r'^news/'), include(news_patterns, namespace='news')),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user