urls.txt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ======================================
  2. ``django.conf.urls`` utility functions
  3. ======================================
  4. .. module:: django.conf.urls
  5. .. versionchanged:: 1.4
  6. Starting with Django 1.4 functions ``patterns``, ``url``, ``include`` plus
  7. the ``handler*`` symbols described below live in the ``django.conf.urls``
  8. module.
  9. Until Django 1.3 they were located in ``django.conf.urls.defaults``. You
  10. still can import them from there but it will be removed in Django 1.6.
  11. patterns()
  12. ----------
  13. .. function:: patterns(prefix, pattern_description, ...)
  14. A function that takes a prefix, and an arbitrary number of URL patterns, and
  15. returns a list of URL patterns in the format Django needs.
  16. The first argument to ``patterns()`` is a string ``prefix``. See
  17. :ref:`The view prefix <urlpatterns-view-prefix>`.
  18. The remaining arguments should be tuples in this format::
  19. (regular expression, Python callback function [, optional_dictionary [, optional_name]])
  20. The ``optional_dictionary`` and ``optional_name`` parameters are described in
  21. :ref:`Passing extra options to view functions <views-extra-options>`.
  22. .. note::
  23. Because `patterns()` is a function call, it accepts a maximum of 255
  24. arguments (URL patterns, in this case). This is a limit for all Python
  25. function calls. This is rarely a problem in practice, because you'll
  26. typically structure your URL patterns modularly by using `include()`
  27. sections. However, on the off-chance you do hit the 255-argument limit,
  28. realize that `patterns()` returns a Python list, so you can split up the
  29. construction of the list.
  30. ::
  31. urlpatterns = patterns('',
  32. ...
  33. )
  34. urlpatterns += patterns('',
  35. ...
  36. )
  37. Python lists have unlimited size, so there's no limit to how many URL
  38. patterns you can construct. The only limit is that you can only create 254
  39. at a time (the 255th argument is the initial prefix argument).
  40. url()
  41. -----
  42. .. function:: url(regex, view, kwargs=None, name=None, prefix='')
  43. You can use the ``url()`` function, instead of a tuple, as an argument to
  44. ``patterns()``. This is convenient if you want to specify a name without the
  45. optional extra arguments dictionary. For example::
  46. urlpatterns = patterns('',
  47. url(r'^index/$', index_view, name="main-view"),
  48. ...
  49. )
  50. This function takes five arguments, most of which are optional::
  51. url(regex, view, kwargs=None, name=None, prefix='')
  52. See :ref:`Naming URL patterns <naming-url-patterns>` for why the ``name``
  53. parameter is useful.
  54. The ``prefix`` parameter has the same meaning as the first argument to
  55. ``patterns()`` and is only relevant when you're passing a string as the
  56. ``view`` parameter.
  57. include()
  58. ---------
  59. .. function:: include(module[, namespace=None, app_name=None])
  60. include(pattern_list)
  61. include((pattern_list, app_namespace, instance_namespace))
  62. A function that takes a full Python import path to another URLconf module
  63. that should be "included" in this place. Optionally, the :term:`application
  64. namespace` and :term:`instance namespace` where the entries will be included
  65. into can also be specified.
  66. ``include()`` also accepts as an argument either an iterable that returns
  67. URL patterns or a 3-tuple containing such iterable plus the names of the
  68. application and instance namespaces.
  69. :arg module: URLconf module (or module name)
  70. :type module: Module or string
  71. :arg namespace: Instance namespace for the URL entries being included
  72. :type namespace: string
  73. :arg app_name: Application namespace for the URL entries being included
  74. :type app_name: string
  75. :arg pattern_list: Iterable of URL entries as returned by :func:`patterns`
  76. :arg app_namespace: Application namespace for the URL entries being included
  77. :type app_namespace: string
  78. :arg instance_namespace: Instance namespace for the URL entries being included
  79. :type instance_namespace: string
  80. See :ref:`including-other-urlconfs` and :ref:`namespaces-and-include`.
  81. handler403
  82. ----------
  83. .. data:: handler403
  84. A callable, or a string representing the full Python import path to the view
  85. that should be called if the user doesn't have the permissions required to
  86. access a resource.
  87. By default, this is ``'django.views.defaults.permission_denied'``. That default
  88. value should suffice.
  89. See the documentation about :ref:`the 403 (HTTP Forbidden) view
  90. <http_forbidden_view>` for more information.
  91. .. versionadded:: 1.4
  92. ``handler403`` is new in Django 1.4.
  93. handler404
  94. ----------
  95. .. data:: handler404
  96. A callable, or a string representing the full Python import path to the view
  97. that should be called if none of the URL patterns match.
  98. By default, this is ``'django.views.defaults.page_not_found'``. That default
  99. value should suffice.
  100. See the documentation about :ref:`the 404 (HTTP Not Found) view
  101. <http_not_found_view>` for more information.
  102. handler500
  103. ----------
  104. .. data:: handler500
  105. A callable, or a string representing the full Python import path to the view
  106. that should be called in case of server errors. Server errors happen when you
  107. have runtime errors in view code.
  108. By default, this is ``'django.views.defaults.server_error'``. That default
  109. value should suffice.
  110. See the documentation about :ref:`the 500 (HTTP Internal Server Error) view
  111. <http_internal_server_error_view>` for more information.