urls.txt 7.2 KB

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