urls.txt 7.1 KB

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