2
0

urls.txt 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. ======================================
  2. ``django.conf.urls`` utility functions
  3. ======================================
  4. .. module:: django.conf.urls
  5. ``patterns()``
  6. ==============
  7. .. function:: patterns(prefix, pattern_description, ...)
  8. .. deprecated:: 1.8
  9. ``urlpatterns`` should be a plain list of :func:`django.conf.urls.url`
  10. instances instead.
  11. A function that takes a prefix, and an arbitrary number of URL patterns, and
  12. returns a list of URL patterns in the format Django needs.
  13. The first argument to ``patterns()`` is a string ``prefix``. Here's the example
  14. URLconf from the :doc:`Django overview </intro/overview>`::
  15. from django.conf.urls import patterns, url
  16. urlpatterns = patterns('',
  17. url(r'^articles/([0-9]{4})/$', 'news.views.year_archive'),
  18. url(r'^articles/([0-9]{4})/([0-9]{2})/$', 'news.views.month_archive'),
  19. url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', 'news.views.article_detail'),
  20. )
  21. In this example, each view has a common prefix -- ``'news.views'``.
  22. Instead of typing that out for each entry in ``urlpatterns``, you can use the
  23. first argument to the ``patterns()`` function to specify a prefix to apply to
  24. each view function.
  25. With this in mind, the above example can be written more concisely as::
  26. from django.conf.urls import patterns, url
  27. urlpatterns = patterns('news.views',
  28. url(r'^articles/([0-9]{4})/$', 'year_archive'),
  29. url(r'^articles/([0-9]{4})/([0-9]{2})/$', 'month_archive'),
  30. url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', 'article_detail'),
  31. )
  32. Note that you don't put a trailing dot (``"."``) in the prefix. Django puts
  33. that in automatically.
  34. The remaining arguments should be tuples in this format::
  35. (regular expression, Python callback function [, optional_dictionary [, optional_name]])
  36. The ``optional_dictionary`` and ``optional_name`` parameters are described in
  37. :ref:`Passing extra options to view functions <views-extra-options>`.
  38. .. note::
  39. Because ``patterns()`` is a function call, it accepts a maximum of 255
  40. arguments (URL patterns, in this case). This is a limit for all Python
  41. function calls. This is rarely a problem in practice, because you'll
  42. typically structure your URL patterns modularly by using ``include()``
  43. sections. However, on the off-chance you do hit the 255-argument limit,
  44. realize that ``patterns()`` returns a Python list, so you can split up the
  45. construction of the list.
  46. ::
  47. urlpatterns = patterns('',
  48. ...
  49. )
  50. urlpatterns += patterns('',
  51. ...
  52. )
  53. Python lists have unlimited size, so there's no limit to how many URL
  54. patterns you can construct. The only limit is that you can only create 254
  55. at a time (the 255th argument is the initial prefix argument).
  56. ``static()``
  57. ============
  58. .. function:: static.static(prefix, view=django.views.static.serve, **kwargs)
  59. Helper function to return a URL pattern for serving files in debug mode::
  60. from django.conf import settings
  61. from django.conf.urls.static import static
  62. urlpatterns = [
  63. # ... the rest of your URLconf goes here ...
  64. ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  65. .. versionchanged:: 1.8
  66. The ``view`` argument changed from a string
  67. (``'django.views.static.serve'``) to the function.
  68. ``url()``
  69. =========
  70. .. function:: url(regex, view, kwargs=None, name=None, prefix='')
  71. ``urlpatterns`` should be a list of ``url()`` instances. For example::
  72. urlpatterns = [
  73. url(r'^index/$', index_view, name="main-view"),
  74. ...
  75. ]
  76. This function takes five arguments, most of which are optional::
  77. url(regex, view, kwargs=None, name=None, prefix='')
  78. The ``kwargs`` parameter allows you to pass additional arguments to the view
  79. function or method. See :ref:`views-extra-options` for an example.
  80. See :ref:`Naming URL patterns <naming-url-patterns>` for why the ``name``
  81. parameter is useful.
  82. .. deprecated:: 1.8
  83. Support for string ``view`` arguments is deprecated and will be removed in
  84. Django 1.10. Pass the callable instead.
  85. The ``prefix`` parameter has the same meaning as the first argument to
  86. ``patterns()`` and is only relevant when you're passing a string as the
  87. ``view`` parameter.
  88. ``include()``
  89. =============
  90. .. function:: include(module, namespace=None, app_name=None)
  91. include(pattern_list)
  92. include((pattern_list, app_namespace), namespace=None)
  93. include((pattern_list, app_namespace, instance_namespace))
  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. Usually, the application namespace should be specified by the included
  99. module. If an application namespace is set, the ``namespace`` argument
  100. can be used to set a different instance namespace.
  101. ``include()`` also accepts as an argument either an iterable that returns
  102. URL patterns, a 2-tuple containing such iterable plus the names of the
  103. application namespaces, or a 3-tuple containing the iterable and the names
  104. of both the application and instance namespace.
  105. :arg module: URLconf module (or module name)
  106. :arg namespace: Instance namespace for the URL entries being included
  107. :type namespace: string
  108. :arg app_name: Application namespace for the URL entries being included
  109. :type app_name: string
  110. :arg pattern_list: Iterable of :func:`django.conf.urls.url` instances
  111. :arg app_namespace: Application namespace for the URL entries being included
  112. :type app_namespace: string
  113. :arg instance_namespace: Instance namespace for the URL entries being included
  114. :type instance_namespace: string
  115. See :ref:`including-other-urlconfs` and :ref:`namespaces-and-include`.
  116. .. deprecated:: 1.9
  117. Support for the ``app_name`` argument is deprecated and will be removed in
  118. Django 2.0. Specify the ``app_name`` as explained in
  119. :ref:`namespaces-and-include` instead.
  120. Support for passing a 3-tuple is also deprecated and will be removed in
  121. Django 2.0. Pass a 2-tuple containing the pattern list and application
  122. namespace, and use the ``namespace`` argument instead.
  123. Lastly, support for an instance namespace without an application namespace
  124. has been deprecated and will be removed in Django 2.0. Specify the
  125. application namespace or remove the instance namespace.
  126. ``handler400``
  127. ==============
  128. .. data:: handler400
  129. A callable, or a string representing the full Python import path to the view
  130. that should be called if the HTTP client has sent a request that caused an error
  131. condition and a response with a status code of 400.
  132. By default, this is ``'django.views.defaults.bad_request'``. If you
  133. implement a custom view, be sure it returns an
  134. :class:`~django.http.HttpResponseBadRequest`.
  135. See the documentation about :ref:`the 400 (bad request) view
  136. <http_bad_request_view>` for more information.
  137. ``handler403``
  138. ==============
  139. .. data:: handler403
  140. A callable, or a string representing the full Python import path to the view
  141. that should be called if the user doesn't have the permissions required to
  142. access a resource.
  143. By default, this is ``'django.views.defaults.permission_denied'``. If you
  144. implement a custom view, be sure it returns an
  145. :class:`~django.http.HttpResponseForbidden`.
  146. See the documentation about :ref:`the 403 (HTTP Forbidden) view
  147. <http_forbidden_view>` for more information.
  148. ``handler404``
  149. ==============
  150. .. data:: handler404
  151. A callable, or a string representing the full Python import path to the view
  152. that should be called if none of the URL patterns match.
  153. By default, this is ``'django.views.defaults.page_not_found'``. If you
  154. implement a custom view, be sure it returns an
  155. :class:`~django.http.HttpResponseNotFound`.
  156. See the documentation about :ref:`the 404 (HTTP Not Found) view
  157. <http_not_found_view>` for more information.
  158. ``handler500``
  159. ==============
  160. .. data:: handler500
  161. A callable, or a string representing the full Python import path to the view
  162. that should be called in case of server errors. Server errors happen when you
  163. have runtime errors in view code.
  164. By default, this is ``'django.views.defaults.server_error'``. If you
  165. implement a custom view, be sure it returns an
  166. :class:`~django.http.HttpResponseServerError`.
  167. See the documentation about :ref:`the 500 (HTTP Internal Server Error) view
  168. <http_internal_server_error_view>` for more information.