2012-09-28 06:16:49 +08:00
|
|
|
======================================
|
|
|
|
``django.conf.urls`` utility functions
|
|
|
|
======================================
|
|
|
|
|
|
|
|
.. module:: django.conf.urls
|
|
|
|
|
|
|
|
patterns()
|
|
|
|
----------
|
|
|
|
|
|
|
|
.. function:: patterns(prefix, pattern_description, ...)
|
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
.. deprecated:: 1.8
|
|
|
|
|
|
|
|
``urlpatterns`` should be a plain list of :func:`django.conf.urls.url`
|
|
|
|
instances instead.
|
|
|
|
|
2012-09-28 06:16:49 +08:00
|
|
|
A function that takes a prefix, and an arbitrary number of URL patterns, and
|
|
|
|
returns a list of URL patterns in the format Django needs.
|
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
The first argument to ``patterns()`` is a string ``prefix``. Here's the example
|
|
|
|
URLconf from the :doc:`Django overview </intro/overview>`::
|
|
|
|
|
|
|
|
from django.conf.urls import patterns, url
|
|
|
|
|
|
|
|
urlpatterns = patterns('',
|
2014-04-15 02:12:44 +08:00
|
|
|
url(r'^articles/([0-9]{4})/$', 'news.views.year_archive'),
|
|
|
|
url(r'^articles/([0-9]{4})/([0-9]{2})/$', 'news.views.month_archive'),
|
|
|
|
url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', 'news.views.article_detail'),
|
2014-04-02 08:46:34 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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',
|
2014-04-15 02:12:44 +08:00
|
|
|
url(r'^articles/([0-9]{4})/$', 'year_archive'),
|
|
|
|
url(r'^articles/([0-9]{4})/([0-9]{2})/$', 'month_archive'),
|
|
|
|
url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', 'article_detail'),
|
2014-04-02 08:46:34 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
Note that you don't put a trailing dot (``"."``) in the prefix. Django puts
|
|
|
|
that in automatically.
|
2012-09-28 06:16:49 +08:00
|
|
|
|
|
|
|
The remaining arguments should be tuples in this format::
|
|
|
|
|
|
|
|
(regular expression, Python callback function [, optional_dictionary [, optional_name]])
|
|
|
|
|
|
|
|
The ``optional_dictionary`` and ``optional_name`` parameters are described in
|
|
|
|
:ref:`Passing extra options to view functions <views-extra-options>`.
|
|
|
|
|
|
|
|
.. note::
|
2013-03-22 17:50:45 +08:00
|
|
|
Because ``patterns()`` is a function call, it accepts a maximum of 255
|
2012-09-28 06:16:49 +08:00
|
|
|
arguments (URL patterns, in this case). This is a limit for all Python
|
|
|
|
function calls. This is rarely a problem in practice, because you'll
|
2013-03-22 17:50:45 +08:00
|
|
|
typically structure your URL patterns modularly by using ``include()``
|
2012-09-28 06:16:49 +08:00
|
|
|
sections. However, on the off-chance you do hit the 255-argument limit,
|
2013-03-22 17:50:45 +08:00
|
|
|
realize that ``patterns()`` returns a Python list, so you can split up the
|
2012-09-28 06:16:49 +08:00
|
|
|
construction of the list.
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
urlpatterns = patterns('',
|
|
|
|
...
|
|
|
|
)
|
|
|
|
urlpatterns += patterns('',
|
|
|
|
...
|
|
|
|
)
|
|
|
|
|
|
|
|
Python lists have unlimited size, so there's no limit to how many URL
|
|
|
|
patterns you can construct. The only limit is that you can only create 254
|
|
|
|
at a time (the 255th argument is the initial prefix argument).
|
|
|
|
|
2013-03-08 03:15:39 +08:00
|
|
|
static()
|
|
|
|
--------
|
|
|
|
|
2014-08-12 22:54:42 +08:00
|
|
|
.. function:: static.static(prefix, view=django.views.static.serve, **kwargs)
|
2013-03-08 03:15:39 +08:00
|
|
|
|
|
|
|
Helper function to return a URL pattern for serving files in debug mode::
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.conf.urls.static import static
|
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2013-03-08 03:15:39 +08:00
|
|
|
# ... the rest of your URLconf goes here ...
|
2014-04-02 08:46:34 +08:00
|
|
|
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
2013-03-08 03:15:39 +08:00
|
|
|
|
2014-08-12 22:54:42 +08:00
|
|
|
.. versionchanged:: 1.8
|
|
|
|
|
|
|
|
The ``view`` argument changed from a string
|
|
|
|
(``'django.views.static.serve'``) to the function.
|
|
|
|
|
2012-09-28 06:16:49 +08:00
|
|
|
url()
|
|
|
|
-----
|
|
|
|
|
|
|
|
.. function:: url(regex, view, kwargs=None, name=None, prefix='')
|
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
``urlpatterns`` should be a list of ``url()`` instances. For example::
|
2012-09-28 06:16:49 +08:00
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2012-09-28 06:16:49 +08:00
|
|
|
url(r'^index/$', index_view, name="main-view"),
|
|
|
|
...
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2012-09-28 06:16:49 +08:00
|
|
|
|
|
|
|
This function takes five arguments, most of which are optional::
|
|
|
|
|
|
|
|
url(regex, view, kwargs=None, name=None, prefix='')
|
|
|
|
|
2014-03-26 05:06:54 +08:00
|
|
|
The ``kwargs`` parameter allows you to pass additional arguments to the view
|
|
|
|
function or method. See :ref:`views-extra-options` for an example.
|
|
|
|
|
2012-09-28 06:16:49 +08:00
|
|
|
See :ref:`Naming URL patterns <naming-url-patterns>` for why the ``name``
|
|
|
|
parameter is useful.
|
|
|
|
|
2014-08-12 22:54:42 +08:00
|
|
|
.. deprecated:: 1.8
|
|
|
|
|
|
|
|
Support for string ``view`` arguments is deprecated and will be removed in
|
|
|
|
Django 2.0. Pass the callable instead.
|
|
|
|
|
|
|
|
The ``prefix`` parameter has the same meaning as the first argument to
|
|
|
|
``patterns()`` and is only relevant when you're passing a string as the
|
|
|
|
``view`` parameter.
|
2012-09-28 06:16:49 +08:00
|
|
|
|
|
|
|
include()
|
|
|
|
---------
|
|
|
|
|
2012-10-07 03:19:51 +08:00
|
|
|
.. function:: include(module[, namespace=None, app_name=None])
|
|
|
|
include(pattern_list)
|
2015-05-28 23:25:52 +08:00
|
|
|
include((pattern_list, app_namespace)[, namespace=None])
|
2012-10-07 03:19:51 +08:00
|
|
|
include((pattern_list, app_namespace, instance_namespace))
|
|
|
|
|
|
|
|
A function that takes a full Python import path to another URLconf module
|
|
|
|
that should be "included" in this place. Optionally, the :term:`application
|
|
|
|
namespace` and :term:`instance namespace` where the entries will be included
|
|
|
|
into can also be specified.
|
|
|
|
|
2015-05-28 23:25:52 +08:00
|
|
|
Usually, the application namespace should be specified by the included
|
|
|
|
module. If an application namespace is set, the ``namespace`` argument
|
|
|
|
can be used to set a different instance namespace.
|
|
|
|
|
2012-10-07 03:19:51 +08:00
|
|
|
``include()`` also accepts as an argument either an iterable that returns
|
2015-05-28 23:25:52 +08:00
|
|
|
URL patterns, a 2-tuple containing such iterable plus the names of the
|
|
|
|
application namespaces, or a 3-tuple containing the iterable and the names
|
|
|
|
of both the application and instance namespace.
|
2012-10-07 03:19:51 +08:00
|
|
|
|
|
|
|
:arg module: URLconf module (or module name)
|
|
|
|
:arg namespace: Instance namespace for the URL entries being included
|
|
|
|
:type namespace: string
|
|
|
|
:arg app_name: Application namespace for the URL entries being included
|
|
|
|
:type app_name: string
|
2014-04-02 08:46:34 +08:00
|
|
|
:arg pattern_list: Iterable of :func:`django.conf.urls.url` instances
|
2012-10-07 03:19:51 +08:00
|
|
|
:arg app_namespace: Application namespace for the URL entries being included
|
|
|
|
:type app_namespace: string
|
|
|
|
:arg instance_namespace: Instance namespace for the URL entries being included
|
|
|
|
:type instance_namespace: string
|
|
|
|
|
|
|
|
See :ref:`including-other-urlconfs` and :ref:`namespaces-and-include`.
|
2012-09-28 06:16:49 +08:00
|
|
|
|
2015-05-28 23:25:52 +08:00
|
|
|
.. deprecated:: 1.9
|
|
|
|
|
|
|
|
Support for the ``app_name`` argument is deprecated and will be removed in
|
|
|
|
Django 2.1. Specify the ``app_name`` as explained in
|
|
|
|
:ref:`namespaces-and-include` instead.
|
|
|
|
|
|
|
|
Support for passing a 3-tuple is also deprecated and will be removed in
|
|
|
|
Django 2.1. Pass a 2-tuple containing the pattern list and application
|
|
|
|
namespace, and use the ``namespace`` argument instead.
|
|
|
|
|
|
|
|
Lastly, support for an instance namespace without an application namespace
|
|
|
|
has been deprecated and will be removed in Django 2.1. Specify the
|
|
|
|
application namespace or remove the instance namespace.
|
|
|
|
|
2013-09-22 22:21:09 +08:00
|
|
|
handler400
|
|
|
|
----------
|
|
|
|
|
|
|
|
.. data:: handler400
|
|
|
|
|
|
|
|
A callable, or a string representing the full Python import path to the view
|
|
|
|
that should be called if the HTTP client has sent a request that caused an error
|
|
|
|
condition and a response with a status code of 400.
|
|
|
|
|
2014-03-24 04:17:52 +08:00
|
|
|
By default, this is ``'django.views.defaults.bad_request'``. That default
|
2013-09-22 22:21:09 +08:00
|
|
|
value should suffice.
|
|
|
|
|
|
|
|
See the documentation about :ref:`the 400 (bad request) view
|
|
|
|
<http_bad_request_view>` for more information.
|
|
|
|
|
2012-09-28 06:16:49 +08:00
|
|
|
handler403
|
|
|
|
----------
|
|
|
|
|
|
|
|
.. data:: handler403
|
|
|
|
|
|
|
|
A callable, or a string representing the full Python import path to the view
|
|
|
|
that should be called if the user doesn't have the permissions required to
|
|
|
|
access a resource.
|
|
|
|
|
|
|
|
By default, this is ``'django.views.defaults.permission_denied'``. That default
|
|
|
|
value should suffice.
|
|
|
|
|
|
|
|
See the documentation about :ref:`the 403 (HTTP Forbidden) view
|
|
|
|
<http_forbidden_view>` for more information.
|
|
|
|
|
|
|
|
handler404
|
|
|
|
----------
|
|
|
|
|
|
|
|
.. data:: handler404
|
|
|
|
|
|
|
|
A callable, or a string representing the full Python import path to the view
|
|
|
|
that should be called if none of the URL patterns match.
|
|
|
|
|
|
|
|
By default, this is ``'django.views.defaults.page_not_found'``. That default
|
|
|
|
value should suffice.
|
|
|
|
|
|
|
|
See the documentation about :ref:`the 404 (HTTP Not Found) view
|
|
|
|
<http_not_found_view>` for more information.
|
|
|
|
|
|
|
|
handler500
|
|
|
|
----------
|
|
|
|
|
|
|
|
.. data:: handler500
|
|
|
|
|
|
|
|
A callable, or a string representing the full Python import path to the view
|
|
|
|
that should be called in case of server errors. Server errors happen when you
|
|
|
|
have runtime errors in view code.
|
|
|
|
|
|
|
|
By default, this is ``'django.views.defaults.server_error'``. That default
|
|
|
|
value should suffice.
|
|
|
|
|
|
|
|
See the documentation about :ref:`the 500 (HTTP Internal Server Error) view
|
|
|
|
<http_internal_server_error_view>` for more information.
|