urls.txt 4.9 KB

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