1. =============================================
    
  2. ``django.urls`` functions for use in URLconfs
    
  3. =============================================
    
  4. 
    
  5. .. module:: django.urls.conf
    
  6.     :synopsis: Functions for use in URLconfs.
    
  7. 
    
  8. .. currentmodule:: django.urls
    
  9. 
    
  10. ``path()``
    
  11. ==========
    
  12. 
    
  13. .. function:: path(route, view, kwargs=None, name=None)
    
  14. 
    
  15. Returns an element for inclusion in ``urlpatterns``. For example::
    
  16. 
    
  17.     from django.urls import include, path
    
  18. 
    
  19.     urlpatterns = [
    
  20.         path('index/', views.index, name='main-view'),
    
  21.         path('bio/<username>/', views.bio, name='bio'),
    
  22.         path('articles/<slug:title>/', views.article, name='article-detail'),
    
  23.         path('articles/<slug:title>/<int:section>/', views.section, name='article-section'),
    
  24.         path('blog/', include('blog.urls')),
    
  25.         ...
    
  26.     ]
    
  27. 
    
  28. The ``route`` argument should be a string or
    
  29. :func:`~django.utils.translation.gettext_lazy()` (see
    
  30. :ref:`translating-urlpatterns`) that contains a URL pattern. The string
    
  31. may contain angle brackets (like ``<username>`` above) to capture part of the
    
  32. URL and send it as a keyword argument to the view. The angle brackets may
    
  33. include a converter specification (like the ``int`` part of ``<int:section>``)
    
  34. which limits the characters matched and may also change the type of the
    
  35. variable passed to the view. For example, ``<int:section>`` matches a string
    
  36. of decimal digits and converts the value to an ``int``. See
    
  37. :ref:`how-django-processes-a-request` for more details.
    
  38. 
    
  39. The ``view`` argument is a view function or the result of
    
  40. :meth:`~django.views.generic.base.View.as_view` for class-based views. It can
    
  41. also be an :func:`django.urls.include`.
    
  42. 
    
  43. The ``kwargs`` argument allows you to pass additional arguments to the view
    
  44. function or method. See :ref:`views-extra-options` for an example.
    
  45. 
    
  46. See :ref:`Naming URL patterns <naming-url-patterns>` for why the ``name``
    
  47. argument is useful.
    
  48. 
    
  49. ``re_path()``
    
  50. =============
    
  51. 
    
  52. .. function:: re_path(route, view, kwargs=None, name=None)
    
  53. 
    
  54. Returns an element for inclusion in ``urlpatterns``. For example::
    
  55. 
    
  56.     from django.urls import include, re_path
    
  57. 
    
  58.     urlpatterns = [
    
  59.         re_path(r'^index/$', views.index, name='index'),
    
  60.         re_path(r'^bio/(?P<username>\w+)/$', views.bio, name='bio'),
    
  61.         re_path(r'^blog/', include('blog.urls')),
    
  62.         ...
    
  63.     ]
    
  64. 
    
  65. The ``route`` argument should be a string or
    
  66. :func:`~django.utils.translation.gettext_lazy()` (see
    
  67. :ref:`translating-urlpatterns`) that contains a regular expression compatible
    
  68. with Python's :py:mod:`re` module. Strings typically use raw string syntax
    
  69. (``r''``) so that they can contain sequences like ``\d`` without the need to
    
  70. escape the backslash with another backslash. When a match is made, captured
    
  71. groups from the regular expression are passed to the view -- as named arguments
    
  72. if the groups are named, and as positional arguments otherwise. The values are
    
  73. passed as strings, without any type conversion.
    
  74. 
    
  75. When a ``route`` ends with ``$`` the whole requested URL, matching against
    
  76. :attr:`~django.http.HttpRequest.path_info`, must match the regular expression
    
  77. pattern (:py:func:`re.fullmatch` is used).
    
  78. 
    
  79. The ``view``, ``kwargs`` and ``name`` arguments are the same as for
    
  80. :func:`~django.urls.path()`.
    
  81. 
    
  82. .. versionchanged:: 2.2.25
    
  83. 
    
  84.     In older versions, a full-match wasn't required for a ``route`` which ends
    
  85.     with ``$``.
    
  86. 
    
  87. ``include()``
    
  88. =============
    
  89. 
    
  90. .. function:: include(module, namespace=None)
    
  91.               include(pattern_list)
    
  92.               include((pattern_list, app_namespace), namespace=None)
    
  93. 
    
  94.     A function that takes a full Python import path to another URLconf module
    
  95.     that should be "included" in this place. Optionally, the :term:`application
    
  96.     namespace` and :term:`instance namespace` where the entries will be included
    
  97.     into can also be specified.
    
  98. 
    
  99.     Usually, the application namespace should be specified by the included
    
  100.     module. If an application namespace is set, the ``namespace`` argument
    
  101.     can be used to set a different instance namespace.
    
  102. 
    
  103.     ``include()`` also accepts as an argument either an iterable that returns
    
  104.     URL patterns or a 2-tuple containing such iterable plus the names of the
    
  105.     application namespaces.
    
  106. 
    
  107.     :arg module: URLconf module (or module name)
    
  108.     :arg namespace: Instance namespace for the URL entries being included
    
  109.     :type namespace: str
    
  110.     :arg pattern_list: Iterable of :func:`~django.urls.path` and/or :func:`~django.urls.re_path` instances.
    
  111.     :arg app_namespace: Application namespace for the URL entries being included
    
  112.     :type app_namespace: str
    
  113. 
    
  114. See :ref:`including-other-urlconfs` and :ref:`namespaces-and-include`.
    
  115. 
    
  116. ``register_converter()``
    
  117. ========================
    
  118. 
    
  119. .. function:: register_converter(converter, type_name)
    
  120. 
    
  121. The function for registering a converter for use in :func:`~django.urls.path()`
    
  122. ``route``\s.
    
  123. 
    
  124. The ``converter`` argument is a converter class, and ``type_name`` is the
    
  125. converter name to use in path patterns. See
    
  126. :ref:`registering-custom-path-converters` for an example.
    
  127. 
    
  128. ==================================================
    
  129. ``django.conf.urls`` functions for use in URLconfs
    
  130. ==================================================
    
  131. 
    
  132. .. module:: django.conf.urls
    
  133. 
    
  134. ``static()``
    
  135. ============
    
  136. 
    
  137. .. function:: static.static(prefix, view=django.views.static.serve, **kwargs)
    
  138. 
    
  139. Helper function to return a URL pattern for serving files in debug mode::
    
  140. 
    
  141.     from django.conf import settings
    
  142.     from django.conf.urls.static import static
    
  143. 
    
  144.     urlpatterns = [
    
  145.         # ... the rest of your URLconf goes here ...
    
  146.     ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    
  147. 
    
  148. ``handler400``
    
  149. ==============
    
  150. 
    
  151. .. data:: handler400
    
  152. 
    
  153. A callable, or a string representing the full Python import path to the view
    
  154. that should be called if the HTTP client has sent a request that caused an error
    
  155. condition and a response with a status code of 400.
    
  156. 
    
  157. By default, this is :func:`django.views.defaults.bad_request`. If you
    
  158. implement a custom view, be sure it accepts ``request`` and ``exception``
    
  159. arguments and returns an :class:`~django.http.HttpResponseBadRequest`.
    
  160. 
    
  161. ``handler403``
    
  162. ==============
    
  163. 
    
  164. .. data:: handler403
    
  165. 
    
  166. A callable, or a string representing the full Python import path to the view
    
  167. that should be called if the user doesn't have the permissions required to
    
  168. access a resource.
    
  169. 
    
  170. By default, this is :func:`django.views.defaults.permission_denied`. If you
    
  171. implement a custom view, be sure it accepts ``request`` and ``exception``
    
  172. arguments and returns an :class:`~django.http.HttpResponseForbidden`.
    
  173. 
    
  174. ``handler404``
    
  175. ==============
    
  176. 
    
  177. .. data:: handler404
    
  178. 
    
  179. A callable, or a string representing the full Python import path to the view
    
  180. that should be called if none of the URL patterns match.
    
  181. 
    
  182. By default, this is :func:`django.views.defaults.page_not_found`. If you
    
  183. implement a custom view, be sure it accepts ``request`` and ``exception``
    
  184. arguments and returns an :class:`~django.http.HttpResponseNotFound`.
    
  185. 
    
  186. ``handler500``
    
  187. ==============
    
  188. 
    
  189. .. data:: handler500
    
  190. 
    
  191. A callable, or a string representing the full Python import path to the view
    
  192. that should be called in case of server errors. Server errors happen when you
    
  193. have runtime errors in view code.
    
  194. 
    
  195. By default, this is :func:`django.views.defaults.server_error`. If you
    
  196. implement a custom view, be sure it accepts a ``request`` argument and returns
    
  197. an :class:`~django.http.HttpResponseServerError`.