urls.txt 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. ``include()``
  63. =============
  64. .. function:: include(module, namespace=None)
  65. include(pattern_list)
  66. include((pattern_list, app_namespace), namespace=None)
  67. A function that takes a full Python import path to another URLconf module
  68. that should be "included" in this place. Optionally, the :term:`application
  69. namespace` and :term:`instance namespace` where the entries will be included
  70. into can also be specified.
  71. Usually, the application namespace should be specified by the included
  72. module. If an application namespace is set, the ``namespace`` argument
  73. can be used to set a different instance namespace.
  74. ``include()`` also accepts as an argument either an iterable that returns
  75. URL patterns or a 2-tuple containing such iterable plus the names of the
  76. application namespaces.
  77. :arg module: URLconf module (or module name)
  78. :arg namespace: Instance namespace for the URL entries being included
  79. :type namespace: str
  80. :arg pattern_list: Iterable of :func:`~django.urls.path` and/or :func:`~django.urls.re_path` instances.
  81. :arg app_namespace: Application namespace for the URL entries being included
  82. :type app_namespace: str
  83. See :ref:`including-other-urlconfs` and :ref:`namespaces-and-include`.
  84. ``register_converter()``
  85. ========================
  86. .. function:: register_converter(converter, type_name)
  87. The function for registering a converter for use in :func:`~django.urls.path()`
  88. ``route``\s.
  89. The ``converter`` argument is a converter class, and ``type_name`` is the
  90. converter name to use in path patterns. See
  91. :ref:`registering-custom-path-converters` for an example.
  92. .. deprecated:: 5.1
  93. Overriding existing converters is deprecated.
  94. ==================================================
  95. ``django.conf.urls`` functions for use in URLconfs
  96. ==================================================
  97. .. module:: django.conf.urls
  98. ``static()``
  99. ============
  100. .. function:: static.static(prefix, view=django.views.static.serve, **kwargs)
  101. Helper function to return a URL pattern for serving files in debug mode::
  102. from django.conf import settings
  103. from django.conf.urls.static import static
  104. urlpatterns = [
  105. # ... the rest of your URLconf goes here ...
  106. ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  107. ``handler400``
  108. ==============
  109. .. data:: handler400
  110. A callable, or a string representing the full Python import path to the view
  111. that should be called if the HTTP client has sent a request that caused an error
  112. condition and a response with a status code of 400.
  113. By default, this is :func:`django.views.defaults.bad_request`. If you
  114. implement a custom view, be sure it accepts ``request`` and ``exception``
  115. arguments and returns an :class:`~django.http.HttpResponseBadRequest`.
  116. ``handler403``
  117. ==============
  118. .. data:: handler403
  119. A callable, or a string representing the full Python import path to the view
  120. that should be called if the user doesn't have the permissions required to
  121. access a resource.
  122. By default, this is :func:`django.views.defaults.permission_denied`. If you
  123. implement a custom view, be sure it accepts ``request`` and ``exception``
  124. arguments and returns an :class:`~django.http.HttpResponseForbidden`.
  125. ``handler404``
  126. ==============
  127. .. data:: handler404
  128. A callable, or a string representing the full Python import path to the view
  129. that should be called if none of the URL patterns match.
  130. By default, this is :func:`django.views.defaults.page_not_found`. If you
  131. implement a custom view, be sure it accepts ``request`` and ``exception``
  132. arguments and returns an :class:`~django.http.HttpResponseNotFound`.
  133. ``handler500``
  134. ==============
  135. .. data:: handler500
  136. A callable, or a string representing the full Python import path to the view
  137. that should be called in case of server errors. Server errors happen when you
  138. have runtime errors in view code.
  139. By default, this is :func:`django.views.defaults.server_error`. If you
  140. implement a custom view, be sure it accepts a ``request`` argument and returns
  141. an :class:`~django.http.HttpResponseServerError`.