mirror of https://github.com/django/django.git
148 lines
5.0 KiB
Plaintext
148 lines
5.0 KiB
Plaintext
=============================================
|
|
``django.urls`` functions for use in URLconfs
|
|
=============================================
|
|
|
|
.. module:: django.urls.conf
|
|
:synopsis: Functions for use in URLconfs.
|
|
|
|
.. currentmodule:: django.conf.urls
|
|
|
|
``include()``
|
|
=============
|
|
|
|
.. function:: include(module, namespace=None)
|
|
include(pattern_list)
|
|
include((pattern_list, app_namespace), namespace=None)
|
|
|
|
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.
|
|
|
|
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.
|
|
|
|
``include()`` also accepts as an argument either an iterable that returns
|
|
URL patterns or a 2-tuple containing such iterable plus the names of the
|
|
application namespaces.
|
|
|
|
:arg module: URLconf module (or module name)
|
|
:arg namespace: Instance namespace for the URL entries being included
|
|
:type namespace: string
|
|
:arg pattern_list: Iterable of :func:`django.conf.urls.url` instances
|
|
: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`.
|
|
|
|
.. versionchanged:: 2.0
|
|
|
|
In older versions, this function is located in ``django.conf.urls``. The
|
|
old location still works for backwards compatibility.
|
|
|
|
==================================================
|
|
``django.conf.urls`` functions for use in URLconfs
|
|
==================================================
|
|
|
|
.. module:: django.conf.urls
|
|
|
|
``static()``
|
|
============
|
|
|
|
.. function:: static.static(prefix, view=django.views.static.serve, **kwargs)
|
|
|
|
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
|
|
|
|
urlpatterns = [
|
|
# ... the rest of your URLconf goes here ...
|
|
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
|
|
|
``url()``
|
|
=========
|
|
|
|
.. function:: url(regex, view, kwargs=None, name=None)
|
|
|
|
``urlpatterns`` should be a list of ``url()`` instances. For example::
|
|
|
|
from django.conf.urls import include, url
|
|
|
|
urlpatterns = [
|
|
url(r'^index/$', index_view, name='main-view'),
|
|
url(r'^weblog/', include('blog.urls')),
|
|
...
|
|
]
|
|
|
|
The ``regex`` parameter should be a string or
|
|
:func:`~django.utils.translation.gettext_lazy()` (see
|
|
: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.
|
|
|
|
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`.
|
|
|
|
The ``kwargs`` parameter allows you to pass additional arguments to the view
|
|
function or method. See :ref:`views-extra-options` for an example.
|
|
|
|
See :ref:`Naming URL patterns <naming-url-patterns>` for why the ``name``
|
|
parameter is useful.
|
|
|
|
``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.
|
|
|
|
By default, this is :func:`django.views.defaults.bad_request`. If you
|
|
implement a custom view, be sure it returns an
|
|
:class:`~django.http.HttpResponseBadRequest`.
|
|
|
|
``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 :func:`django.views.defaults.permission_denied`. If you
|
|
implement a custom view, be sure it returns an
|
|
:class:`~django.http.HttpResponseForbidden`.
|
|
|
|
``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 :func:`django.views.defaults.page_not_found`. If you
|
|
implement a custom view, be sure it returns an
|
|
:class:`~django.http.HttpResponseNotFound`.
|
|
|
|
``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 :func:`django.views.defaults.server_error`. If you
|
|
implement a custom view, be sure it returns an
|
|
:class:`~django.http.HttpResponseServerError`.
|