2012-09-27 22:16:49 +00:00
|
|
|
======================================
|
|
|
|
``django.conf.urls`` utility functions
|
|
|
|
======================================
|
|
|
|
|
|
|
|
.. module:: django.conf.urls
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``static()``
|
|
|
|
============
|
2013-03-07 19:15:39 +00:00
|
|
|
|
2014-08-12 14:54:42 +00:00
|
|
|
.. function:: static.static(prefix, view=django.views.static.serve, **kwargs)
|
2013-03-07 19:15:39 +00: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 00:46:34 +00:00
|
|
|
urlpatterns = [
|
2013-03-07 19:15:39 +00:00
|
|
|
# ... the rest of your URLconf goes here ...
|
2014-04-02 00:46:34 +00:00
|
|
|
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
2013-03-07 19:15:39 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``url()``
|
|
|
|
=========
|
2012-09-27 22:16:49 +00:00
|
|
|
|
2015-08-17 15:07:03 +00:00
|
|
|
.. function:: url(regex, view, kwargs=None, name=None)
|
2012-09-27 22:16:49 +00:00
|
|
|
|
2014-04-02 00:46:34 +00:00
|
|
|
``urlpatterns`` should be a list of ``url()`` instances. For example::
|
2012-09-27 22:16:49 +00:00
|
|
|
|
2016-06-20 09:07:59 +00:00
|
|
|
from django.conf.urls import include, url
|
|
|
|
|
2014-04-02 00:46:34 +00:00
|
|
|
urlpatterns = [
|
2016-06-20 09:07:59 +00:00
|
|
|
url(r'^index/$', index_view, name='main-view'),
|
|
|
|
url(r'^weblog/', include('blog.urls')),
|
2012-09-27 22:16:49 +00:00
|
|
|
...
|
2014-04-02 00:46:34 +00:00
|
|
|
]
|
2012-09-27 22:16:49 +00:00
|
|
|
|
2016-12-23 00:16:26 +00:00
|
|
|
The ``regex`` parameter should be a string or
|
2017-01-26 19:58:33 +00:00
|
|
|
:func:`~django.utils.translation.gettext_lazy()` (see
|
2016-12-23 00:16:26 +00:00
|
|
|
:ref:`translating-urlpatterns`) that contains a regular expression compatible
|
|
|
|
with Python's :py:mod:`re` module. Strings typically use raw string syntax
|
|
|
|
(``r''``) so that they can contain sequences like ``\d`` without the need to
|
|
|
|
escape the backslash with another backslash.
|
|
|
|
|
2016-06-20 09:07:59 +00:00
|
|
|
The ``view`` parameter is a view function or the result of
|
|
|
|
:meth:`~django.views.generic.base.View.as_view` for class-based views. It can
|
|
|
|
also be an :func:`include`.
|
|
|
|
|
2014-03-25 21:06:54 +00: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-27 22:16:49 +00:00
|
|
|
See :ref:`Naming URL patterns <naming-url-patterns>` for why the ``name``
|
|
|
|
parameter is useful.
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``include()``
|
|
|
|
=============
|
2012-09-27 22:16:49 +00:00
|
|
|
|
2017-01-10 14:57:49 +00:00
|
|
|
.. function:: include(module, namespace=None)
|
2012-10-06 19:19:51 +00:00
|
|
|
include(pattern_list)
|
2015-07-27 12:35:21 +00:00
|
|
|
include((pattern_list, app_namespace), namespace=None)
|
2012-10-06 19:19:51 +00:00
|
|
|
|
|
|
|
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 15:25:52 +00: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-06 19:19:51 +00:00
|
|
|
``include()`` also accepts as an argument either an iterable that returns
|
2017-01-10 14:57:49 +00:00
|
|
|
URL patterns or a 2-tuple containing such iterable plus the names of the
|
|
|
|
application namespaces.
|
2012-10-06 19:19:51 +00:00
|
|
|
|
|
|
|
:arg module: URLconf module (or module name)
|
|
|
|
:arg namespace: Instance namespace for the URL entries being included
|
|
|
|
:type namespace: string
|
2014-04-02 00:46:34 +00:00
|
|
|
:arg pattern_list: Iterable of :func:`django.conf.urls.url` instances
|
2012-10-06 19:19:51 +00: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-27 22:16:49 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``handler400``
|
|
|
|
==============
|
2013-09-22 14:21:09 +00:00
|
|
|
|
|
|
|
.. 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.
|
|
|
|
|
2017-01-27 21:13:40 +00:00
|
|
|
By default, this is :func:`django.views.defaults.bad_request`. If you
|
2016-01-05 01:11:20 +00:00
|
|
|
implement a custom view, be sure it returns an
|
|
|
|
:class:`~django.http.HttpResponseBadRequest`.
|
2013-09-22 14:21:09 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``handler403``
|
|
|
|
==============
|
2012-09-27 22:16:49 +00:00
|
|
|
|
|
|
|
.. 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.
|
|
|
|
|
2017-01-27 21:13:40 +00:00
|
|
|
By default, this is :func:`django.views.defaults.permission_denied`. If you
|
2016-01-05 01:11:20 +00:00
|
|
|
implement a custom view, be sure it returns an
|
|
|
|
:class:`~django.http.HttpResponseForbidden`.
|
2012-09-27 22:16:49 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``handler404``
|
|
|
|
==============
|
2012-09-27 22:16:49 +00:00
|
|
|
|
|
|
|
.. 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.
|
|
|
|
|
2017-01-27 21:13:40 +00:00
|
|
|
By default, this is :func:`django.views.defaults.page_not_found`. If you
|
2016-01-05 01:11:20 +00:00
|
|
|
implement a custom view, be sure it returns an
|
|
|
|
:class:`~django.http.HttpResponseNotFound`.
|
2012-09-27 22:16:49 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``handler500``
|
|
|
|
==============
|
2012-09-27 22:16:49 +00:00
|
|
|
|
|
|
|
.. 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.
|
|
|
|
|
2017-01-27 21:13:40 +00:00
|
|
|
By default, this is :func:`django.views.defaults.server_error`. If you
|
2016-01-05 01:11:20 +00:00
|
|
|
implement a custom view, be sure it returns an
|
|
|
|
:class:`~django.http.HttpResponseServerError`.
|