urls.txt 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 2.0. 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, instance_namespace))
  93. A function that takes a full Python import path to another URLconf module
  94. that should be "included" in this place. Optionally, the :term:`application
  95. namespace` and :term:`instance namespace` where the entries will be included
  96. into can also be specified.
  97. ``include()`` also accepts as an argument either an iterable that returns
  98. URL patterns or a 3-tuple containing such iterable plus the names of the
  99. application and instance namespaces.
  100. :arg module: URLconf module (or module name)
  101. :arg namespace: Instance namespace for the URL entries being included
  102. :type namespace: string
  103. :arg app_name: Application namespace for the URL entries being included
  104. :type app_name: string
  105. :arg pattern_list: Iterable of :func:`django.conf.urls.url` instances
  106. :arg app_namespace: Application namespace for the URL entries being included
  107. :type app_namespace: string
  108. :arg instance_namespace: Instance namespace for the URL entries being included
  109. :type instance_namespace: string
  110. See :ref:`including-other-urlconfs` and :ref:`namespaces-and-include`.
  111. handler400
  112. ----------
  113. .. data:: handler400
  114. A callable, or a string representing the full Python import path to the view
  115. that should be called if the HTTP client has sent a request that caused an error
  116. condition and a response with a status code of 400.
  117. By default, this is ``'django.views.defaults.bad_request'``. That default
  118. value should suffice.
  119. See the documentation about :ref:`the 400 (bad request) view
  120. <http_bad_request_view>` for more information.
  121. handler403
  122. ----------
  123. .. data:: handler403
  124. A callable, or a string representing the full Python import path to the view
  125. that should be called if the user doesn't have the permissions required to
  126. access a resource.
  127. By default, this is ``'django.views.defaults.permission_denied'``. That default
  128. value should suffice.
  129. See the documentation about :ref:`the 403 (HTTP Forbidden) view
  130. <http_forbidden_view>` for more information.
  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 ``'django.views.defaults.page_not_found'``. That default
  137. value should suffice.
  138. See the documentation about :ref:`the 404 (HTTP Not Found) view
  139. <http_not_found_view>` for more information.
  140. handler500
  141. ----------
  142. .. data:: handler500
  143. A callable, or a string representing the full Python import path to the view
  144. that should be called in case of server errors. Server errors happen when you
  145. have runtime errors in view code.
  146. By default, this is ``'django.views.defaults.server_error'``. That default
  147. value should suffice.
  148. See the documentation about :ref:`the 500 (HTTP Internal Server Error) view
  149. <http_internal_server_error_view>` for more information.