2005-07-20 09:16:51 +08:00
|
|
|
==============
|
|
|
|
URL dispatcher
|
|
|
|
==============
|
2005-07-16 05:17:52 +08:00
|
|
|
|
2005-11-20 02:20:30 +08:00
|
|
|
A clean, elegant URL scheme is an important detail in a high-quality Web
|
|
|
|
application. Django lets you design URLs however you want, with no framework
|
|
|
|
limitations.
|
2005-07-16 05:17:52 +08:00
|
|
|
|
2005-11-20 02:20:30 +08:00
|
|
|
See `Cool URIs don't change`_, by World Wide Web creator Tim Berners-Lee, for
|
|
|
|
excellent arguments on why URLs should be clean and usable.
|
|
|
|
|
2018-01-07 21:28:41 +08:00
|
|
|
.. _Cool URIs don't change: https://www.w3.org/Provider/Style/URI
|
2005-11-20 02:20:30 +08:00
|
|
|
|
|
|
|
Overview
|
|
|
|
========
|
|
|
|
|
|
|
|
To design URLs for an app, you create a Python module informally called a
|
2012-09-08 07:12:14 +08:00
|
|
|
**URLconf** (URL configuration). This module is pure Python code and is a
|
2016-10-21 01:29:04 +08:00
|
|
|
mapping between URL path expressions to Python functions (your views).
|
2005-11-20 02:20:30 +08:00
|
|
|
|
|
|
|
This mapping can be as short or as long as needed. It can reference other
|
|
|
|
mappings. And, because it's pure Python code, it can be constructed
|
|
|
|
dynamically.
|
|
|
|
|
2012-12-27 04:47:29 +08:00
|
|
|
Django also provides a way to translate URLs according to the active
|
|
|
|
language. See the :ref:`internationalization documentation
|
|
|
|
<url-internationalization>` for more information.
|
2011-07-04 17:52:06 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. _how-django-processes-a-request:
|
|
|
|
|
2005-11-20 02:20:30 +08:00
|
|
|
How Django processes a request
|
|
|
|
==============================
|
|
|
|
|
|
|
|
When a user requests a page from your Django-powered site, this is the
|
|
|
|
algorithm the system follows to determine which Python code to execute:
|
|
|
|
|
2018-11-16 02:54:28 +08:00
|
|
|
#. Django determines the root URLconf module to use. Ordinarily,
|
2011-10-14 08:12:01 +08:00
|
|
|
this is the value of the :setting:`ROOT_URLCONF` setting, but if the incoming
|
2015-06-20 19:05:35 +08:00
|
|
|
``HttpRequest`` object has a :attr:`~django.http.HttpRequest.urlconf`
|
2015-11-07 23:12:37 +08:00
|
|
|
attribute (set by middleware), its value will be used in place of the
|
|
|
|
:setting:`ROOT_URLCONF` setting.
|
2009-07-11 23:38:47 +08:00
|
|
|
|
2018-11-16 02:54:28 +08:00
|
|
|
#. Django loads that Python module and looks for the variable
|
2019-05-06 23:54:11 +08:00
|
|
|
``urlpatterns``. This should be a :term:`sequence` of
|
|
|
|
:func:`django.urls.path` and/or :func:`django.urls.re_path` instances.
|
2009-07-11 23:38:47 +08:00
|
|
|
|
2018-11-16 02:54:28 +08:00
|
|
|
#. Django runs through each URL pattern, in order, and stops at the first
|
2020-01-24 19:21:04 +08:00
|
|
|
one that matches the requested URL, matching against
|
|
|
|
:attr:`~django.http.HttpRequest.path_info`.
|
2009-07-11 23:38:47 +08:00
|
|
|
|
2018-11-16 02:54:28 +08:00
|
|
|
#. Once one of the URL patterns matches, Django imports and calls the given
|
2019-06-17 22:54:55 +08:00
|
|
|
view, which is a Python function (or a :doc:`class-based view
|
2014-03-26 05:06:54 +08:00
|
|
|
</topics/class-based-views/index>`). The view gets passed the following
|
|
|
|
arguments:
|
|
|
|
|
|
|
|
* An instance of :class:`~django.http.HttpRequest`.
|
2019-12-06 16:32:51 +08:00
|
|
|
* If the matched URL pattern contained no named groups, then the
|
2014-03-26 05:06:54 +08:00
|
|
|
matches from the regular expression are provided as positional arguments.
|
2016-10-21 01:29:04 +08:00
|
|
|
* The keyword arguments are made up of any named parts matched by the
|
2019-12-11 18:57:27 +08:00
|
|
|
path expression that are provided, overridden by any arguments specified
|
|
|
|
in the optional ``kwargs`` argument to :func:`django.urls.path` or
|
2016-10-21 01:29:04 +08:00
|
|
|
:func:`django.urls.re_path`.
|
2005-11-20 02:20:30 +08:00
|
|
|
|
2018-11-16 02:54:28 +08:00
|
|
|
#. If no URL pattern matches, or if an exception is raised during any
|
2011-10-14 08:12:01 +08:00
|
|
|
point in this process, Django invokes an appropriate
|
|
|
|
error-handling view. See `Error handling`_ below.
|
2011-09-11 12:01:41 +08:00
|
|
|
|
2005-11-20 02:20:30 +08:00
|
|
|
Example
|
|
|
|
=======
|
|
|
|
|
|
|
|
Here's a sample URLconf::
|
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import path
|
2005-11-20 02:20:30 +08:00
|
|
|
|
2014-08-12 22:54:42 +08:00
|
|
|
from . import views
|
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path('articles/2003/', views.special_case_2003),
|
|
|
|
path('articles/<int:year>/', views.year_archive),
|
|
|
|
path('articles/<int:year>/<int:month>/', views.month_archive),
|
2017-09-26 01:06:11 +08:00
|
|
|
path('articles/<int:year>/<int:month>/<slug:slug>/', views.article_detail),
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2005-11-20 02:20:30 +08:00
|
|
|
|
|
|
|
Notes:
|
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
* To capture a value from the URL, use angle brackets.
|
2005-11-20 02:20:30 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
* Captured values can optionally include a converter type. For example, use
|
|
|
|
``<int:name>`` to capture an integer parameter. If a converter isn't included,
|
|
|
|
any string, excluding a ``/`` character, is matched.
|
2005-11-27 22:35:18 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
* There's no need to add a leading slash, because every URL has that. For
|
|
|
|
example, it's ``articles``, not ``/articles``.
|
2005-11-20 02:20:30 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
Example requests:
|
2005-11-20 02:20:30 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* A request to ``/articles/2005/03/`` would match the third entry in the
|
|
|
|
list. Django would call the function
|
2016-10-21 01:29:04 +08:00
|
|
|
``views.month_archive(request, year=2005, month=3)``.
|
2005-11-20 02:20:30 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``/articles/2003/`` would match the first pattern in the list, not the
|
|
|
|
second one, because the patterns are tested in order, and the first one
|
|
|
|
is the first test to pass. Feel free to exploit the ordering to insert
|
2015-05-09 10:49:24 +08:00
|
|
|
special cases like this. Here, Django would call the function
|
|
|
|
``views.special_case_2003(request)``
|
2005-11-20 02:20:30 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``/articles/2003`` would not match any of these patterns, because each
|
|
|
|
pattern requires that the URL end with a slash.
|
2005-11-20 02:20:30 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
* ``/articles/2003/03/building-a-django-site/`` would match the final
|
|
|
|
pattern. Django would call the function
|
|
|
|
``views.article_detail(request, year=2003, month=3, slug="building-a-django-site")``.
|
|
|
|
|
|
|
|
Path converters
|
|
|
|
===============
|
|
|
|
|
|
|
|
The following path converters are available by default:
|
|
|
|
|
|
|
|
* ``str`` - Matches any non-empty string, excluding the path separator, ``'/'``.
|
|
|
|
This is the default if a converter isn't included in the expression.
|
|
|
|
|
2020-03-31 16:37:38 +08:00
|
|
|
* ``int`` - Matches zero or any positive integer. Returns an ``int``.
|
2016-10-21 01:29:04 +08:00
|
|
|
|
|
|
|
* ``slug`` - Matches any slug string consisting of ASCII letters or numbers,
|
|
|
|
plus the hyphen and underscore characters. For example,
|
|
|
|
``building-your-1st-django-site``.
|
|
|
|
|
2017-12-28 01:23:44 +08:00
|
|
|
* ``uuid`` - Matches a formatted UUID. To prevent multiple URLs from mapping to
|
|
|
|
the same page, dashes must be included and letters must be lowercase. For
|
|
|
|
example, ``075194d3-6885-417e-a8a8-6c931e272f00``. Returns a
|
|
|
|
:class:`~uuid.UUID` instance.
|
2016-10-21 01:29:04 +08:00
|
|
|
|
|
|
|
* ``path`` - Matches any non-empty string, including the path separator,
|
|
|
|
``'/'``. This allows you to match against a complete URL path rather than
|
2019-06-17 22:54:55 +08:00
|
|
|
a segment of a URL path as with ``str``.
|
2016-10-21 01:29:04 +08:00
|
|
|
|
|
|
|
.. _registering-custom-path-converters:
|
|
|
|
|
|
|
|
Registering custom path converters
|
|
|
|
==================================
|
|
|
|
|
|
|
|
For more complex matching requirements, you can define your own path converters.
|
|
|
|
|
|
|
|
A converter is a class that includes the following:
|
|
|
|
|
|
|
|
* A ``regex`` class attribute, as a string.
|
|
|
|
|
|
|
|
* A ``to_python(self, value)`` method, which handles converting the matched
|
|
|
|
string into the type that should be passed to the view function. It should
|
2019-07-04 02:00:18 +08:00
|
|
|
raise ``ValueError`` if it can't convert the given value. A ``ValueError`` is
|
|
|
|
interpreted as no match and as a consequence a 404 response is sent to the
|
2019-12-22 02:09:47 +08:00
|
|
|
user unless another URL pattern matches.
|
2016-10-21 01:29:04 +08:00
|
|
|
|
|
|
|
* A ``to_url(self, value)`` method, which handles converting the Python type
|
2019-12-22 02:22:18 +08:00
|
|
|
into a string to be used in the URL. It should raise ``ValueError`` if it
|
|
|
|
can't convert the given value. A ``ValueError`` is interpreted as no match
|
|
|
|
and as a consequence :func:`~django.urls.reverse` will raise
|
|
|
|
:class:`~django.urls.NoReverseMatch` unless another URL pattern matches.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.1
|
|
|
|
|
|
|
|
Support for raising ``ValueError`` to indicate no match was added.
|
2016-10-21 01:29:04 +08:00
|
|
|
|
|
|
|
For example::
|
|
|
|
|
|
|
|
class FourDigitYearConverter:
|
|
|
|
regex = '[0-9]{4}'
|
|
|
|
|
|
|
|
def to_python(self, value):
|
|
|
|
return int(value)
|
|
|
|
|
|
|
|
def to_url(self, value):
|
|
|
|
return '%04d' % value
|
|
|
|
|
|
|
|
Register custom converter classes in your URLconf using
|
|
|
|
:func:`~django.urls.register_converter`::
|
|
|
|
|
2018-05-13 01:37:42 +08:00
|
|
|
from django.urls import path, register_converter
|
2005-11-20 02:20:30 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
from . import converters, views
|
2005-11-20 02:20:30 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
register_converter(converters.FourDigitYearConverter, 'yyyy')
|
2005-11-28 07:03:56 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
urlpatterns = [
|
|
|
|
path('articles/2003/', views.special_case_2003),
|
|
|
|
path('articles/<yyyy:year>/', views.year_archive),
|
|
|
|
...
|
|
|
|
]
|
2005-11-28 07:03:56 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
Using regular expressions
|
|
|
|
=========================
|
|
|
|
|
|
|
|
If the paths and converters syntax isn't sufficient for defining your URL
|
|
|
|
patterns, you can also use regular expressions. To do so, use
|
|
|
|
:func:`~django.urls.re_path` instead of :func:`~django.urls.path`.
|
|
|
|
|
|
|
|
In Python regular expressions, the syntax for named regular expression groups
|
2005-11-28 07:03:56 +08:00
|
|
|
is ``(?P<name>pattern)``, where ``name`` is the name of the group and
|
|
|
|
``pattern`` is some pattern to match.
|
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
Here's the example URLconf from earlier, rewritten using regular expressions::
|
2005-11-28 07:03:56 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import path, re_path
|
2013-05-18 20:00:52 +08:00
|
|
|
|
2014-08-12 22:54:42 +08:00
|
|
|
from . import views
|
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path('articles/2003/', views.special_case_2003),
|
2018-02-21 10:31:30 +08:00
|
|
|
re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
|
|
|
|
re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive),
|
2018-02-27 08:57:52 +08:00
|
|
|
re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<slug>[\w-]+)/$', views.article_detail),
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2005-11-28 07:03:56 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
This accomplishes roughly the same thing as the previous example, except:
|
|
|
|
|
|
|
|
* The exact URLs that will match are slightly more constrained. For example,
|
|
|
|
the year 10000 will no longer match since the year integers are constrained
|
|
|
|
to be exactly four digits long.
|
|
|
|
|
|
|
|
* Each captured argument is sent to the view as a string, regardless of what
|
|
|
|
sort of match the regular expression makes.
|
|
|
|
|
|
|
|
When switching from using :func:`~django.urls.path` to
|
|
|
|
:func:`~django.urls.re_path` or vice versa, it's particularly important to be
|
|
|
|
aware that the type of the view arguments may change, and so you may need to
|
|
|
|
adapt your views.
|
|
|
|
|
|
|
|
Using unnamed regular expression groups
|
|
|
|
---------------------------------------
|
|
|
|
|
|
|
|
As well as the named group syntax, e.g. ``(?P<year>[0-9]{4})``, you can
|
|
|
|
also use the shorter unnamed group, e.g. ``([0-9]{4})``.
|
|
|
|
|
|
|
|
This usage isn't particularly recommended as it makes it easier to accidentally
|
|
|
|
introduce errors between the intended meaning of a match and the arguments
|
|
|
|
of the view.
|
|
|
|
|
2017-10-31 09:47:09 +08:00
|
|
|
In either case, using only one style within a given regex is recommended. When
|
2016-10-21 01:29:04 +08:00
|
|
|
both styles are mixed, any unnamed groups are ignored and only named groups are
|
|
|
|
passed to the view function.
|
2005-11-28 07:03:56 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
Nested arguments
|
|
|
|
----------------
|
2005-11-28 07:03:56 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
Regular expressions allow nested arguments, and Django will resolve them and
|
|
|
|
pass them to the view. When reversing, Django will try to fill in all outer
|
|
|
|
captured arguments, ignoring any nested captured arguments. Consider the
|
|
|
|
following URL patterns which optionally take a page argument::
|
2005-11-28 07:03:56 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import re_path
|
2005-11-28 07:03:56 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
urlpatterns = [
|
2018-02-21 10:31:30 +08:00
|
|
|
re_path(r'^blog/(page-(\d+)/)?$', blog_articles), # bad
|
|
|
|
re_path(r'^comments/(?:page-(?P<page_number>\d+)/)?$', comments), # good
|
2016-10-21 01:29:04 +08:00
|
|
|
]
|
2005-11-28 07:03:56 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
Both patterns use nested arguments and will resolve: for example,
|
|
|
|
``blog/page-2/`` will result in a match to ``blog_articles`` with two
|
|
|
|
positional arguments: ``page-2/`` and ``2``. The second pattern for
|
|
|
|
``comments`` will match ``comments/page-2/`` with keyword argument
|
|
|
|
``page_number`` set to 2. The outer argument in this case is a non-capturing
|
|
|
|
argument ``(?:...)``.
|
2005-11-28 07:03:56 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
The ``blog_articles`` view needs the outermost captured argument to be reversed,
|
|
|
|
``page-2/`` or no arguments in this case, while ``comments`` can be reversed
|
|
|
|
with either no arguments or a value for ``page_number``.
|
2005-11-28 07:03:56 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
Nested captured arguments create a strong coupling between the view arguments
|
|
|
|
and the URL as illustrated by ``blog_articles``: the view receives part of the
|
|
|
|
URL (``page-2/``) instead of only the value the view is interested in. This
|
|
|
|
coupling is even more pronounced when reversing, since to reverse the view we
|
|
|
|
need to pass the piece of URL instead of the page number.
|
2012-09-08 07:12:14 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
As a rule of thumb, only capture the values the view needs to work with and
|
|
|
|
use non-capturing arguments when the regular expression needs an argument but
|
|
|
|
the view ignores it.
|
2005-11-28 07:03:56 +08:00
|
|
|
|
2005-11-20 02:20:30 +08:00
|
|
|
What the URLconf searches against
|
|
|
|
=================================
|
|
|
|
|
|
|
|
The URLconf searches against the requested URL, as a normal Python string. This
|
|
|
|
does not include GET or POST parameters, or the domain name.
|
|
|
|
|
2015-11-30 00:29:46 +08:00
|
|
|
For example, in a request to ``https://www.example.com/myapp/``, the URLconf
|
2008-05-29 20:02:11 +08:00
|
|
|
will look for ``myapp/``.
|
2005-11-20 02:20:30 +08:00
|
|
|
|
2015-11-30 00:29:46 +08:00
|
|
|
In a request to ``https://www.example.com/myapp/?page=3``, the URLconf will look
|
2008-05-29 20:02:11 +08:00
|
|
|
for ``myapp/``.
|
2005-11-20 02:20:30 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
The URLconf doesn't look at the request method. In other words, all request
|
|
|
|
methods -- ``POST``, ``GET``, ``HEAD``, etc. -- will be routed to the same
|
|
|
|
function for the same URL.
|
|
|
|
|
2013-12-31 06:55:12 +08:00
|
|
|
Specifying defaults for view arguments
|
|
|
|
======================================
|
|
|
|
|
2012-09-08 07:12:14 +08:00
|
|
|
A convenient trick is to specify default parameters for your views' arguments.
|
|
|
|
Here's an example URLconf and view::
|
|
|
|
|
|
|
|
# URLconf
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import path
|
2013-05-18 20:00:52 +08:00
|
|
|
|
2014-08-12 22:54:42 +08:00
|
|
|
from . import views
|
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path('blog/', views.page),
|
|
|
|
path('blog/page<int:num>/', views.page),
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2012-09-08 07:12:14 +08:00
|
|
|
|
|
|
|
# View (in blog/views.py)
|
2016-10-21 01:29:04 +08:00
|
|
|
def page(request, num=1):
|
2012-09-08 07:12:14 +08:00
|
|
|
# Output the appropriate page of blog entries, according to num.
|
2013-04-11 08:38:25 +08:00
|
|
|
...
|
2012-09-08 07:12:14 +08:00
|
|
|
|
|
|
|
In the above example, both URL patterns point to the same view --
|
2014-08-12 22:54:42 +08:00
|
|
|
``views.page`` -- but the first pattern doesn't capture anything from the
|
2012-09-08 07:12:14 +08:00
|
|
|
URL. If the first pattern matches, the ``page()`` function will use its
|
2016-10-21 01:29:04 +08:00
|
|
|
default argument for ``num``, ``1``. If the second pattern matches,
|
|
|
|
``page()`` will use whatever ``num`` value was captured.
|
2012-09-08 07:12:14 +08:00
|
|
|
|
|
|
|
Performance
|
|
|
|
===========
|
|
|
|
|
|
|
|
Each regular expression in a ``urlpatterns`` is compiled the first time it's
|
|
|
|
accessed. This makes the system blazingly fast.
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
Syntax of the ``urlpatterns`` variable
|
|
|
|
======================================
|
2005-11-20 02:20:30 +08:00
|
|
|
|
2019-05-06 23:54:11 +08:00
|
|
|
``urlpatterns`` should be a :term:`sequence` of :func:`~django.urls.path`
|
|
|
|
and/or :func:`~django.urls.re_path` instances.
|
2005-11-20 02:20:30 +08:00
|
|
|
|
2011-09-11 12:01:41 +08:00
|
|
|
Error handling
|
|
|
|
==============
|
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
When Django can't find a match for the requested URL, or when an exception is
|
|
|
|
raised, Django invokes an error-handling view.
|
2011-09-11 12:01:41 +08:00
|
|
|
|
2013-09-22 22:21:09 +08:00
|
|
|
The views to use for these cases are specified by four variables. Their
|
2012-09-28 06:16:49 +08:00
|
|
|
default values should suffice for most projects, but further customization is
|
2015-05-09 10:49:24 +08:00
|
|
|
possible by overriding their default values.
|
2011-09-11 12:01:41 +08:00
|
|
|
|
2012-09-28 06:16:49 +08:00
|
|
|
See the documentation on :ref:`customizing error views
|
|
|
|
<customizing-error-views>` for the full details.
|
2011-09-12 06:36:16 +08:00
|
|
|
|
2012-09-28 06:16:49 +08:00
|
|
|
Such values can be set in your root URLconf. Setting these variables in any
|
|
|
|
other URLconf will have no effect.
|
2011-09-12 06:36:16 +08:00
|
|
|
|
2012-09-28 06:16:49 +08:00
|
|
|
Values must be callables, or strings representing the full Python import path
|
|
|
|
to the view that should be called to handle the error condition at hand.
|
2011-09-12 06:36:16 +08:00
|
|
|
|
2012-09-28 06:16:49 +08:00
|
|
|
The variables are:
|
2011-09-12 06:36:16 +08:00
|
|
|
|
2015-05-09 10:49:24 +08:00
|
|
|
* ``handler400`` -- See :data:`django.conf.urls.handler400`.
|
|
|
|
* ``handler403`` -- See :data:`django.conf.urls.handler403`.
|
2012-09-28 06:16:49 +08:00
|
|
|
* ``handler404`` -- See :data:`django.conf.urls.handler404`.
|
|
|
|
* ``handler500`` -- See :data:`django.conf.urls.handler500`.
|
2011-09-12 06:36:16 +08:00
|
|
|
|
2012-09-28 06:16:49 +08:00
|
|
|
.. _including-other-urlconfs:
|
|
|
|
|
2005-07-20 09:16:51 +08:00
|
|
|
Including other URLconfs
|
|
|
|
========================
|
2005-07-16 05:17:52 +08:00
|
|
|
|
2005-11-20 02:20:30 +08:00
|
|
|
At any point, your ``urlpatterns`` can "include" other URLconf modules. This
|
|
|
|
essentially "roots" a set of URLs below other ones.
|
|
|
|
|
2015-11-15 20:05:15 +08:00
|
|
|
For example, here's an excerpt of the URLconf for the `Django website`_
|
2012-02-17 05:59:22 +08:00
|
|
|
itself. It includes a number of other URLconfs::
|
2005-07-16 05:17:52 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import include, path
|
2005-07-20 09:16:51 +08:00
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2012-02-17 05:59:22 +08:00
|
|
|
# ... snip ...
|
2016-10-21 01:29:04 +08:00
|
|
|
path('community/', include('aggregator.urls')),
|
|
|
|
path('contact/', include('contact.urls')),
|
2012-02-17 05:59:22 +08:00
|
|
|
# ... snip ...
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2005-07-20 09:16:51 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
Whenever Django encounters :func:`~django.urls.include()`, it chops off
|
|
|
|
whatever part of the URL matched up to that point and sends the remaining
|
2012-09-28 06:16:49 +08:00
|
|
|
string to the included URLconf for further processing.
|
2005-11-27 22:35:18 +08:00
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
Another possibility is to include additional URL patterns by using a list of
|
2016-10-21 01:29:04 +08:00
|
|
|
:func:`~django.urls.path` instances. For example, consider this URLconf::
|
2009-07-11 23:38:47 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import include, path
|
2009-07-11 23:38:47 +08:00
|
|
|
|
2014-08-12 22:54:42 +08:00
|
|
|
from apps.main import views as main_views
|
|
|
|
from credit import views as credit_views
|
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
extra_patterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path('reports/', credit_views.report),
|
|
|
|
path('reports/<int:id>/', credit_views.report),
|
|
|
|
path('charge/', credit_views.charge),
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2009-07-11 23:38:47 +08:00
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path('', main_views.homepage),
|
|
|
|
path('help/', include('apps.help.urls')),
|
|
|
|
path('credit/', include(extra_patterns)),
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2009-07-11 23:38:47 +08:00
|
|
|
|
2012-10-07 03:19:51 +08:00
|
|
|
In this example, the ``/credit/reports/`` URL will be handled by the
|
2015-05-09 10:49:24 +08:00
|
|
|
``credit_views.report()`` Django view.
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2013-09-21 23:22:45 +08:00
|
|
|
This can be used to remove redundancy from URLconfs where a single pattern
|
|
|
|
prefix is used repeatedly. For example, consider this URLconf::
|
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import path
|
2014-04-02 08:46:34 +08:00
|
|
|
from . import views
|
2013-09-21 23:22:45 +08:00
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path('<page_slug>-<page_id>/history/', views.history),
|
|
|
|
path('<page_slug>-<page_id>/edit/', views.edit),
|
|
|
|
path('<page_slug>-<page_id>/discuss/', views.discuss),
|
|
|
|
path('<page_slug>-<page_id>/permissions/', views.permissions),
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2013-09-21 23:22:45 +08:00
|
|
|
|
|
|
|
We can improve this by stating the common path prefix only once and grouping
|
|
|
|
the suffixes that differ::
|
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import include, path
|
2014-04-02 08:46:34 +08:00
|
|
|
from . import views
|
2013-09-21 23:22:45 +08:00
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path('<page_slug>-<page_id>/', include([
|
|
|
|
path('history/', views.history),
|
|
|
|
path('edit/', views.edit),
|
|
|
|
path('discuss/', views.discuss),
|
|
|
|
path('permissions/', views.permissions),
|
2014-04-02 08:46:34 +08:00
|
|
|
])),
|
|
|
|
]
|
2013-09-21 23:22:45 +08:00
|
|
|
|
2015-11-15 20:05:15 +08:00
|
|
|
.. _`Django website`: https://www.djangoproject.com/
|
2005-11-27 22:35:18 +08:00
|
|
|
|
|
|
|
Captured parameters
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
An included URLconf receives any captured parameters from parent URLconfs, so
|
|
|
|
the following example is valid::
|
2005-09-27 12:24:19 +08:00
|
|
|
|
|
|
|
# In settings/urls/main.py
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import include, path
|
2013-05-18 20:00:52 +08:00
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path('<username>/blog/', include('foo.urls.blog')),
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2005-09-27 12:24:19 +08:00
|
|
|
|
|
|
|
# In foo/urls/blog.py
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import path
|
2014-04-02 08:46:34 +08:00
|
|
|
from . import views
|
2013-05-18 20:00:52 +08:00
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path('', views.blog.index),
|
|
|
|
path('archive/', views.blog.archive),
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2005-09-27 12:24:19 +08:00
|
|
|
|
|
|
|
In the above example, the captured ``"username"`` variable is passed to the
|
|
|
|
included URLconf, as expected.
|
|
|
|
|
2012-09-28 06:16:49 +08:00
|
|
|
.. _views-extra-options:
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2005-07-16 05:17:52 +08:00
|
|
|
Passing extra options to view functions
|
|
|
|
=======================================
|
|
|
|
|
2005-11-20 02:20:30 +08:00
|
|
|
URLconfs have a hook that lets you pass extra arguments to your view functions,
|
|
|
|
as a Python dictionary.
|
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
The :func:`~django.urls.path` function can take an optional third argument
|
2013-04-11 08:38:25 +08:00
|
|
|
which should be a dictionary of extra keyword arguments to pass to the view
|
|
|
|
function.
|
2005-11-20 02:20:30 +08:00
|
|
|
|
|
|
|
For example::
|
2005-07-16 05:17:52 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import path
|
2014-04-02 08:46:34 +08:00
|
|
|
from . import views
|
2013-05-18 20:00:52 +08:00
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path('blog/<int:year>/', views.year_archive, {'foo': 'bar'}),
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2005-11-20 02:20:30 +08:00
|
|
|
|
2012-08-21 00:23:17 +08:00
|
|
|
In this example, for a request to ``/blog/2005/``, Django will call
|
2016-10-21 01:29:04 +08:00
|
|
|
``views.year_archive(request, year=2005, foo='bar')``.
|
2005-11-20 02:20:30 +08:00
|
|
|
|
2012-04-26 03:17:47 +08:00
|
|
|
This technique is used in the
|
2010-08-20 03:27:44 +08:00
|
|
|
:doc:`syndication framework </ref/contrib/syndication>` to pass metadata and
|
2008-08-24 06:25:40 +08:00
|
|
|
options to views.
|
2006-08-01 10:57:08 +08:00
|
|
|
|
2007-02-28 04:11:26 +08:00
|
|
|
.. admonition:: Dealing with conflicts
|
|
|
|
|
|
|
|
It's possible to have a URL pattern which captures named keyword arguments,
|
|
|
|
and also passes arguments with the same names in its dictionary of extra
|
|
|
|
arguments. When this happens, the arguments in the dictionary will be used
|
|
|
|
instead of the arguments captured in the URL.
|
|
|
|
|
2006-08-01 10:57:08 +08:00
|
|
|
Passing extra options to ``include()``
|
|
|
|
--------------------------------------
|
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
Similarly, you can pass extra options to :func:`~django.urls.include` and
|
|
|
|
each line in the included URLconf will be passed the extra options.
|
2006-08-01 10:57:08 +08:00
|
|
|
|
|
|
|
For example, these two URLconf sets are functionally identical:
|
|
|
|
|
|
|
|
Set one::
|
|
|
|
|
|
|
|
# main.py
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import include, path
|
2013-05-18 20:00:52 +08:00
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path('blog/', include('inner'), {'blog_id': 3}),
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2006-08-01 10:57:08 +08:00
|
|
|
|
|
|
|
# inner.py
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import path
|
2014-04-02 08:46:34 +08:00
|
|
|
from mysite import views
|
2013-05-18 20:00:52 +08:00
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path('archive/', views.archive),
|
|
|
|
path('about/', views.about),
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2006-08-01 10:57:08 +08:00
|
|
|
|
|
|
|
Set two::
|
|
|
|
|
|
|
|
# main.py
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import include, path
|
2014-04-02 08:46:34 +08:00
|
|
|
from mysite import views
|
2013-05-18 20:00:52 +08:00
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path('blog/', include('inner')),
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2006-08-01 10:57:08 +08:00
|
|
|
|
|
|
|
# inner.py
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import path
|
2013-05-18 20:00:52 +08:00
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path('archive/', views.archive, {'blog_id': 3}),
|
|
|
|
path('about/', views.about, {'blog_id': 3}),
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2006-08-01 10:57:08 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
as valid. For this reason, this technique is only useful if you're certain that
|
2008-08-24 06:25:40 +08:00
|
|
|
every view in the included URLconf accepts the extra options you're passing.
|
2006-08-11 15:01:29 +08:00
|
|
|
|
2012-10-08 07:11:12 +08:00
|
|
|
Reverse resolution of URLs
|
|
|
|
==========================
|
|
|
|
|
|
|
|
A common need when working on a Django project is the possibility to obtain URLs
|
|
|
|
in their final forms either for embedding in generated content (views and assets
|
|
|
|
URLs, URLs shown to the user, etc.) or for handling of the navigation flow on
|
|
|
|
the server side (redirections, etc.)
|
|
|
|
|
2015-05-09 10:49:24 +08:00
|
|
|
It is strongly desirable to avoid hard-coding these URLs (a laborious,
|
|
|
|
non-scalable and error-prone strategy). Equally dangerous is devising ad-hoc
|
|
|
|
mechanisms to generate URLs that are parallel to the design described by the
|
|
|
|
URLconf, which can result in the production of URLs that become stale over time.
|
2012-10-08 07:11:12 +08:00
|
|
|
|
|
|
|
In other words, what's needed is a DRY mechanism. Among other advantages it
|
2015-05-09 10:49:24 +08:00
|
|
|
would allow evolution of the URL design without having to go over all the
|
2012-10-08 07:11:12 +08:00
|
|
|
project source code to search and replace outdated URLs.
|
|
|
|
|
2015-05-09 10:49:24 +08:00
|
|
|
The primary piece of information we have available to get a URL is an
|
|
|
|
identification (e.g. the name) of the view in charge of handling it. Other
|
2012-10-08 07:11:12 +08:00
|
|
|
pieces of information that necessarily must participate in the lookup of the
|
|
|
|
right URL are the types (positional, keyword) and values of the view arguments.
|
|
|
|
|
|
|
|
Django provides a solution such that the URL mapper is the only repository of
|
|
|
|
the URL design. You feed it with your URLconf and then it can be used in both
|
|
|
|
directions:
|
|
|
|
|
|
|
|
* Starting with a URL requested by the user/browser, it calls the right Django
|
|
|
|
view providing any arguments it might need with their values as extracted from
|
|
|
|
the URL.
|
|
|
|
|
|
|
|
* Starting with the identification of the corresponding Django view plus the
|
|
|
|
values of arguments that would be passed to it, obtain the associated URL.
|
|
|
|
|
|
|
|
The first one is the usage we've been discussing in the previous sections. The
|
|
|
|
second one is what is known as *reverse resolution of URLs*, *reverse URL
|
|
|
|
matching*, *reverse URL lookup*, or simply *URL reversing*.
|
|
|
|
|
|
|
|
Django provides tools for performing URL reversing that match the different
|
|
|
|
layers where URLs are needed:
|
|
|
|
|
|
|
|
* In templates: Using the :ttag:`url` template tag.
|
|
|
|
|
2015-12-30 23:51:16 +08:00
|
|
|
* In Python code: Using the :func:`~django.urls.reverse` function.
|
2012-10-08 07:11:12 +08:00
|
|
|
|
|
|
|
* In higher level code related to handling of URLs of Django model instances:
|
2012-11-20 20:15:16 +08:00
|
|
|
The :meth:`~django.db.models.Model.get_absolute_url` method.
|
2012-10-08 07:11:12 +08:00
|
|
|
|
|
|
|
Examples
|
|
|
|
--------
|
|
|
|
|
|
|
|
Consider again this URLconf entry::
|
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import path
|
2012-10-08 07:11:12 +08:00
|
|
|
|
2014-08-12 22:54:42 +08:00
|
|
|
from . import views
|
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2012-10-12 02:40:38 +08:00
|
|
|
#...
|
2016-10-21 01:29:04 +08:00
|
|
|
path('articles/<int:year>/', views.year_archive, name='news-year-archive'),
|
2012-10-12 02:40:38 +08:00
|
|
|
#...
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2012-10-08 07:11:12 +08:00
|
|
|
|
|
|
|
According to this design, the URL for the archive corresponding to year *nnnn*
|
2016-10-21 01:29:04 +08:00
|
|
|
is ``/articles/<nnnn>/``.
|
2012-10-08 07:11:12 +08:00
|
|
|
|
|
|
|
You can obtain these in template code by using:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
2014-06-03 19:30:14 +08:00
|
|
|
<a href="{% url 'news-year-archive' 2012 %}">2012 Archive</a>
|
2012-10-08 07:11:12 +08:00
|
|
|
{# Or with the year in a template context variable: #}
|
|
|
|
<ul>
|
|
|
|
{% for yearvar in year_list %}
|
2014-06-03 19:30:14 +08:00
|
|
|
<li><a href="{% url 'news-year-archive' yearvar %}">{{ yearvar }} Archive</a></li>
|
2012-10-08 07:11:12 +08:00
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
Or in Python code::
|
|
|
|
|
|
|
|
from django.http import HttpResponseRedirect
|
2018-05-13 01:37:42 +08:00
|
|
|
from django.urls import reverse
|
2012-10-08 07:11:12 +08:00
|
|
|
|
|
|
|
def redirect_to_year(request):
|
|
|
|
# ...
|
|
|
|
year = 2006
|
|
|
|
# ...
|
2014-06-03 19:30:14 +08:00
|
|
|
return HttpResponseRedirect(reverse('news-year-archive', args=(year,)))
|
2012-10-08 07:11:12 +08:00
|
|
|
|
2012-10-12 02:40:38 +08:00
|
|
|
If, for some reason, it was decided that the URLs where content for yearly
|
2012-10-08 07:11:12 +08:00
|
|
|
article archives are published at should be changed then you would only need to
|
|
|
|
change the entry in the URLconf.
|
|
|
|
|
|
|
|
In some scenarios where views are of a generic nature, a many-to-one
|
|
|
|
relationship might exist between URLs and views. For these cases the view name
|
2013-07-28 09:45:25 +08:00
|
|
|
isn't a good enough identifier for it when comes the time of reversing
|
2012-10-08 07:11:12 +08:00
|
|
|
URLs. Read the next section to know about the solution Django provides for this.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. _naming-url-patterns:
|
|
|
|
|
2007-04-01 15:25:20 +08:00
|
|
|
Naming URL patterns
|
|
|
|
===================
|
|
|
|
|
2014-06-03 19:30:14 +08:00
|
|
|
In order to perform URL reversing, you'll need to use **named URL patterns**
|
|
|
|
as done in the examples above. The string used for the URL name can contain any
|
|
|
|
characters you like. You are not restricted to valid Python names.
|
2007-04-01 15:25:20 +08:00
|
|
|
|
2016-11-05 22:48:31 +08:00
|
|
|
When naming URL patterns, choose names that are unlikely to clash with other
|
|
|
|
applications' choice of names. If you call your URL pattern ``comment``
|
|
|
|
and another application does the same thing, the URL that
|
|
|
|
:func:`~django.urls.reverse()` finds depends on whichever pattern is last in
|
|
|
|
your project's ``urlpatterns`` list.
|
2007-04-01 15:25:20 +08:00
|
|
|
|
2014-06-03 19:30:14 +08:00
|
|
|
Putting a prefix on your URL names, perhaps derived from the application
|
2016-11-05 22:48:31 +08:00
|
|
|
name (such as ``myapp-comment`` instead of ``comment``), decreases the chance
|
|
|
|
of collision.
|
|
|
|
|
|
|
|
You can deliberately choose the *same URL name* as another application if you
|
|
|
|
want to override a view. For example, a common use case is to override the
|
|
|
|
:class:`~django.contrib.auth.views.LoginView`. Parts of Django and most
|
|
|
|
third-party apps assume that this view has a URL pattern with the name
|
|
|
|
``login``. If you have a custom login view and give its URL the name ``login``,
|
|
|
|
:func:`~django.urls.reverse()` will find your custom view as long as it's in
|
|
|
|
``urlpatterns`` after ``django.contrib.auth.urls`` is included (if that's
|
|
|
|
included at all).
|
|
|
|
|
|
|
|
You may also use the same name for multiple URL patterns if they differ in
|
|
|
|
their arguments. In addition to the URL name, :func:`~django.urls.reverse()`
|
2019-12-22 02:22:18 +08:00
|
|
|
matches the number of arguments and the names of the keyword arguments. Path
|
|
|
|
converters can also raise ``ValueError`` to indicate no match, see
|
|
|
|
:ref:`registering-custom-path-converters` for details.
|
2007-05-27 20:05:59 +08:00
|
|
|
|
2012-09-28 06:16:49 +08:00
|
|
|
.. _topics-http-defining-url-namespaces:
|
2009-07-17 00:16:13 +08:00
|
|
|
|
|
|
|
URL namespaces
|
2012-09-28 06:16:49 +08:00
|
|
|
==============
|
|
|
|
|
|
|
|
Introduction
|
|
|
|
------------
|
|
|
|
|
2014-08-20 22:12:25 +08:00
|
|
|
URL namespaces allow you to uniquely reverse :ref:`named URL patterns
|
|
|
|
<naming-url-patterns>` even if different applications use the same URL names.
|
|
|
|
It's a good practice for third-party apps to always use namespaced URLs (as we
|
|
|
|
did in the tutorial). Similarly, it also allows you to reverse URLs if multiple
|
|
|
|
instances of an application are deployed. In other words, since multiple
|
|
|
|
instances of a single application will share named URLs, namespaces provide a
|
|
|
|
way to tell these named URLs apart.
|
|
|
|
|
2019-06-17 22:54:55 +08:00
|
|
|
Django applications that make proper use of URL namespacing can be deployed
|
|
|
|
more than once for a particular site. For example :mod:`django.contrib.admin`
|
|
|
|
has an :class:`~django.contrib.admin.AdminSite` class which allows you to
|
|
|
|
:ref:`deploy more than one instance of the admin <multiple-admin-sites>`. In a
|
|
|
|
later example, we'll discuss the idea of deploying the polls application from
|
|
|
|
the tutorial in two different locations so we can serve the same functionality
|
|
|
|
to two different audiences (authors and publishers).
|
2012-09-28 06:16:49 +08:00
|
|
|
|
|
|
|
A URL namespace comes in two parts, both of which are strings:
|
|
|
|
|
|
|
|
.. glossary::
|
|
|
|
|
|
|
|
application namespace
|
|
|
|
This describes the name of the application that is being deployed. Every
|
|
|
|
instance of a single application will have the same application namespace.
|
|
|
|
For example, Django's admin application has the somewhat predictable
|
2012-10-07 03:19:51 +08:00
|
|
|
application namespace of ``'admin'``.
|
2012-09-28 06:16:49 +08:00
|
|
|
|
|
|
|
instance namespace
|
|
|
|
This identifies a specific instance of an application. Instance namespaces
|
|
|
|
should be unique across your entire project. However, an instance namespace
|
|
|
|
can be the same as the application namespace. This is used to specify a
|
2014-08-20 22:12:25 +08:00
|
|
|
default instance of an application. For example, the default Django admin
|
2012-10-07 03:19:51 +08:00
|
|
|
instance has an instance namespace of ``'admin'``.
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2012-10-07 03:19:51 +08:00
|
|
|
Namespaced URLs are specified using the ``':'`` operator. For example, the main
|
|
|
|
index page of the admin application is referenced using ``'admin:index'``. This
|
|
|
|
indicates a namespace of ``'admin'``, and a named URL of ``'index'``.
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2014-08-20 22:12:25 +08:00
|
|
|
Namespaces can also be nested. The named URL ``'sports:polls:index'`` would
|
|
|
|
look for a pattern named ``'index'`` in the namespace ``'polls'`` that is itself
|
|
|
|
defined within the top-level namespace ``'sports'``.
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2012-09-28 06:16:49 +08:00
|
|
|
.. _topics-http-reversing-url-namespaces:
|
|
|
|
|
|
|
|
Reversing namespaced URLs
|
|
|
|
-------------------------
|
|
|
|
|
2014-08-20 22:12:25 +08:00
|
|
|
When given a namespaced URL (e.g. ``'polls:index'``) to resolve, Django splits
|
|
|
|
the fully qualified name into parts and then tries the following lookup:
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2018-11-16 02:54:28 +08:00
|
|
|
#. First, Django looks for a matching :term:`application namespace` (in this
|
2014-08-20 22:12:25 +08:00
|
|
|
example, ``'polls'``). This will yield a list of instances of that
|
2011-10-14 08:12:01 +08:00
|
|
|
application.
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2018-11-16 02:54:28 +08:00
|
|
|
#. If there is a current application defined, Django finds and returns the URL
|
2015-06-26 00:49:50 +08:00
|
|
|
resolver for that instance. The current application can be specified with
|
2015-12-30 23:51:16 +08:00
|
|
|
the ``current_app`` argument to the :func:`~django.urls.reverse()`
|
|
|
|
function.
|
2015-06-26 00:49:50 +08:00
|
|
|
|
|
|
|
The :ttag:`url` template tag uses the namespace of the currently resolved
|
|
|
|
view as the current application in a
|
|
|
|
:class:`~django.template.RequestContext`. You can override this default by
|
|
|
|
setting the current application on the :attr:`request.current_app
|
|
|
|
<django.http.HttpRequest.current_app>` attribute.
|
2014-12-15 00:48:51 +08:00
|
|
|
|
2018-11-16 02:54:28 +08:00
|
|
|
#. If there is no current application, Django looks for a default
|
2011-10-14 08:12:01 +08:00
|
|
|
application instance. The default application instance is the instance
|
2012-10-07 03:19:51 +08:00
|
|
|
that has an :term:`instance namespace` matching the :term:`application
|
2014-08-20 22:12:25 +08:00
|
|
|
namespace` (in this example, an instance of ``polls`` called ``'polls'``).
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2018-11-16 02:54:28 +08:00
|
|
|
#. If there is no default application instance, Django will pick the last
|
2011-10-14 08:12:01 +08:00
|
|
|
deployed instance of the application, whatever its instance name may be.
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2018-11-16 02:54:28 +08:00
|
|
|
#. If the provided namespace doesn't match an :term:`application namespace` in
|
2011-10-14 08:12:01 +08:00
|
|
|
step 1, Django will attempt a direct lookup of the namespace as an
|
2012-10-07 03:19:51 +08:00
|
|
|
:term:`instance namespace`.
|
2009-07-17 00:16:13 +08:00
|
|
|
|
|
|
|
If there are nested namespaces, these steps are repeated for each part of the
|
|
|
|
namespace until only the view name is unresolved. The view name will then be
|
|
|
|
resolved into a URL in the namespace that has been found.
|
|
|
|
|
2012-09-28 06:16:49 +08:00
|
|
|
Example
|
|
|
|
~~~~~~~
|
|
|
|
|
2009-07-17 00:16:13 +08:00
|
|
|
To show this resolution strategy in action, consider an example of two instances
|
2014-08-20 22:12:25 +08:00
|
|
|
of the ``polls`` application from the tutorial: one called ``'author-polls'``
|
|
|
|
and one called ``'publisher-polls'``. Assume we have enhanced that application
|
|
|
|
so that it takes the instance namespace into consideration when creating and
|
|
|
|
displaying polls.
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2018-09-11 01:00:34 +08:00
|
|
|
.. code-block:: python
|
|
|
|
:caption: urls.py
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import include, path
|
2014-08-20 22:12:25 +08:00
|
|
|
|
|
|
|
urlpatterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path('author-polls/', include('polls.urls', namespace='author-polls')),
|
|
|
|
path('publisher-polls/', include('polls.urls', namespace='publisher-polls')),
|
2014-08-20 22:12:25 +08:00
|
|
|
]
|
|
|
|
|
2018-09-11 01:00:34 +08:00
|
|
|
.. code-block:: python
|
|
|
|
:caption: polls/urls.py
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import path
|
2014-08-20 22:12:25 +08:00
|
|
|
|
|
|
|
from . import views
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2015-05-28 23:25:52 +08:00
|
|
|
app_name = 'polls'
|
2014-08-20 22:12:25 +08:00
|
|
|
urlpatterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path('', views.IndexView.as_view(), name='index'),
|
|
|
|
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
|
2014-08-20 22:12:25 +08:00
|
|
|
...
|
|
|
|
]
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2014-08-20 22:12:25 +08:00
|
|
|
Using this setup, the following lookups are possible:
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2014-08-20 22:12:25 +08:00
|
|
|
* If one of the instances is current - say, if we were rendering the detail page
|
|
|
|
in the instance ``'author-polls'`` - ``'polls:index'`` will resolve to the
|
|
|
|
index page of the ``'author-polls'`` instance; i.e. both of the following will
|
|
|
|
result in ``"/author-polls/"``.
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2014-08-20 22:12:25 +08:00
|
|
|
In the method of a class-based view::
|
|
|
|
|
|
|
|
reverse('polls:index', current_app=self.request.resolver_match.namespace)
|
|
|
|
|
|
|
|
and in the template:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
{% url 'polls:index' %}
|
|
|
|
|
|
|
|
* If there is no current instance - say, if we were rendering a page
|
|
|
|
somewhere else on the site - ``'polls:index'`` will resolve to the last
|
|
|
|
registered instance of ``polls``. Since there is no default instance
|
|
|
|
(instance namespace of ``'polls'``), the last instance of ``polls`` that is
|
|
|
|
registered will be used. This would be ``'publisher-polls'`` since it's
|
|
|
|
declared last in the ``urlpatterns``.
|
|
|
|
|
|
|
|
* ``'author-polls:index'`` will always resolve to the index page of the instance
|
|
|
|
``'author-polls'`` (and likewise for ``'publisher-polls'``) .
|
|
|
|
|
|
|
|
If there were also a default instance - i.e., an instance named ``'polls'`` -
|
|
|
|
the only change from above would be in the case where there is no current
|
|
|
|
instance (the second item in the list above). In this case ``'polls:index'``
|
|
|
|
would resolve to the index page of the default instance instead of the instance
|
|
|
|
declared last in ``urlpatterns``.
|
2012-10-07 03:19:51 +08:00
|
|
|
|
|
|
|
.. _namespaces-and-include:
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2012-09-28 06:16:49 +08:00
|
|
|
URL namespaces and included URLconfs
|
|
|
|
------------------------------------
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2015-05-28 23:25:52 +08:00
|
|
|
Application namespaces of included URLconfs can be specified in two ways.
|
2009-04-01 07:34:03 +08:00
|
|
|
|
2015-05-28 23:25:52 +08:00
|
|
|
Firstly, you can set an ``app_name`` attribute in the included URLconf module,
|
|
|
|
at the same level as the ``urlpatterns`` attribute. You have to pass the actual
|
2016-10-21 01:29:04 +08:00
|
|
|
module, or a string reference to the module, to :func:`~django.urls.include`,
|
|
|
|
not the list of ``urlpatterns`` itself.
|
2008-10-06 16:31:32 +08:00
|
|
|
|
2018-09-11 01:00:34 +08:00
|
|
|
.. code-block:: python
|
|
|
|
:caption: polls/urls.py
|
2015-05-28 23:25:52 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import path
|
2015-05-28 23:25:52 +08:00
|
|
|
|
|
|
|
from . import views
|
|
|
|
|
|
|
|
app_name = 'polls'
|
|
|
|
urlpatterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path('', views.IndexView.as_view(), name='index'),
|
|
|
|
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
|
2015-05-28 23:25:52 +08:00
|
|
|
...
|
|
|
|
]
|
|
|
|
|
2018-09-11 01:00:34 +08:00
|
|
|
.. code-block:: python
|
|
|
|
:caption: urls.py
|
2010-08-05 15:09:47 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import include, path
|
2015-05-28 23:25:52 +08:00
|
|
|
|
|
|
|
urlpatterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path('polls/', include('polls.urls')),
|
2015-05-28 23:25:52 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
The URLs defined in ``polls.urls`` will have an application namespace ``polls``.
|
2007-05-27 20:05:59 +08:00
|
|
|
|
2012-09-28 06:16:49 +08:00
|
|
|
Secondly, you can include an object that contains embedded namespace data. If
|
2016-10-21 01:29:04 +08:00
|
|
|
you ``include()`` a list of :func:`~django.urls.path` or
|
|
|
|
:func:`~django.urls.re_path` instances, the URLs contained in that object
|
|
|
|
will be added to the global namespace. However, you can also ``include()`` a
|
|
|
|
2-tuple containing::
|
2010-09-13 03:44:19 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
(<list of path()/re_path() instances>, <application namespace>)
|
2010-09-13 03:44:19 +08:00
|
|
|
|
2012-10-08 07:11:12 +08:00
|
|
|
For example::
|
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import include, path
|
2013-05-18 20:00:52 +08:00
|
|
|
|
2014-08-20 22:12:25 +08:00
|
|
|
from . import views
|
2014-08-12 22:54:42 +08:00
|
|
|
|
2015-05-28 23:25:52 +08:00
|
|
|
polls_patterns = ([
|
2016-10-21 01:29:04 +08:00
|
|
|
path('', views.IndexView.as_view(), name='index'),
|
|
|
|
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
|
2015-05-28 23:25:52 +08:00
|
|
|
], 'polls')
|
|
|
|
|
2016-06-18 09:01:43 +08:00
|
|
|
urlpatterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path('polls/', include(polls_patterns)),
|
2016-06-18 09:01:43 +08:00
|
|
|
]
|
2012-10-08 07:11:12 +08:00
|
|
|
|
2015-05-28 23:25:52 +08:00
|
|
|
This will include the nominated URL patterns into the given application
|
|
|
|
namespace.
|
2012-10-08 07:11:12 +08:00
|
|
|
|
2015-05-28 23:25:52 +08:00
|
|
|
The instance namespace can be specified using the ``namespace`` argument to
|
2016-10-21 01:29:04 +08:00
|
|
|
:func:`~django.urls.include`. If the instance namespace is not specified,
|
2015-05-28 23:25:52 +08:00
|
|
|
it will default to the included URLconf's application namespace. This means
|
|
|
|
it will also be the default instance for that namespace.
|