urls.txt 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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: string
  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: string
  82. :arg instance_namespace: Instance namespace for the URL entries being included
  83. :type instance_namespace: string
  84. See :ref:`including-other-urlconfs` and :ref:`namespaces-and-include`.
  85. .. versionchanged:: 2.0
  86. In older versions, this function is located in ``django.conf.urls``. The
  87. old location still works for backwards compatibility.
  88. ``register_converter()``
  89. ========================
  90. .. function:: register_converter(converter, type_name)
  91. .. versionadded:: 2.0
  92. The function for registering a converter for use in :func:`~django.urls.path()`
  93. ``route``\s.
  94. The ``converter`` argument is a converter class, and ``type_name`` is the
  95. converter name to use in path patterns. See
  96. :ref:`registering-custom-path-converters` for an example.
  97. ==================================================
  98. ``django.conf.urls`` functions for use in URLconfs
  99. ==================================================
  100. .. module:: django.conf.urls
  101. ``static()``
  102. ============
  103. .. function:: static.static(prefix, view=django.views.static.serve, **kwargs)
  104. Helper function to return a URL pattern for serving files in debug mode::
  105. from django.conf import settings
  106. from django.conf.urls.static import static
  107. urlpatterns = [
  108. # ... the rest of your URLconf goes here ...
  109. ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  110. ``url()``
  111. =========
  112. .. function:: url(regex, view, kwargs=None, name=None)
  113. This function is an alias to :func:`django.urls.re_path()`. It's likely to be
  114. deprecated in a future release.
  115. ``handler400``
  116. ==============
  117. .. data:: handler400
  118. A callable, or a string representing the full Python import path to the view
  119. that should be called if the HTTP client has sent a request that caused an error
  120. condition and a response with a status code of 400.
  121. By default, this is :func:`django.views.defaults.bad_request`. If you
  122. implement a custom view, be sure it returns an
  123. :class:`~django.http.HttpResponseBadRequest`.
  124. ``handler403``
  125. ==============
  126. .. data:: handler403
  127. A callable, or a string representing the full Python import path to the view
  128. that should be called if the user doesn't have the permissions required to
  129. access a resource.
  130. By default, this is :func:`django.views.defaults.permission_denied`. If you
  131. implement a custom view, be sure it returns an
  132. :class:`~django.http.HttpResponseForbidden`.
  133. ``handler404``
  134. ==============
  135. .. data:: handler404
  136. A callable, or a string representing the full Python import path to the view
  137. that should be called if none of the URL patterns match.
  138. By default, this is :func:`django.views.defaults.page_not_found`. If you
  139. implement a custom view, be sure it returns an
  140. :class:`~django.http.HttpResponseNotFound`.
  141. ``handler500``
  142. ==============
  143. .. data:: handler500
  144. A callable, or a string representing the full Python import path to the view
  145. that should be called in case of server errors. Server errors happen when you
  146. have runtime errors in view code.
  147. By default, this is :func:`django.views.defaults.server_error`. If you
  148. implement a custom view, be sure it returns an
  149. :class:`~django.http.HttpResponseServerError`.