urls.txt 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 or pattern_list>)
  60. A function that takes a full Python import path to another URLconf module that
  61. should be "included" in this place.
  62. :func:`include` also accepts as an argument an iterable that returns URL
  63. patterns.
  64. See :ref:`Including other URLconfs <including-other-urlconfs>`.
  65. handler403
  66. ----------
  67. .. data:: handler403
  68. A callable, or a string representing the full Python import path to the view
  69. that should be called if the user doesn't have the permissions required to
  70. access a resource.
  71. By default, this is ``'django.views.defaults.permission_denied'``. That default
  72. value should suffice.
  73. See the documentation about :ref:`the 403 (HTTP Forbidden) view
  74. <http_forbidden_view>` for more information.
  75. .. versionadded:: 1.4
  76. ``handler403`` is new in Django 1.4.
  77. handler404
  78. ----------
  79. .. data:: handler404
  80. A callable, or a string representing the full Python import path to the view
  81. that should be called if none of the URL patterns match.
  82. By default, this is ``'django.views.defaults.page_not_found'``. That default
  83. value should suffice.
  84. See the documentation about :ref:`the 404 (HTTP Not Found) view
  85. <http_not_found_view>` for more information.
  86. handler500
  87. ----------
  88. .. data:: handler500
  89. A callable, or a string representing the full Python import path to the view
  90. that should be called in case of server errors. Server errors happen when you
  91. have runtime errors in view code.
  92. By default, this is ``'django.views.defaults.server_error'``. That default
  93. value should suffice.
  94. See the documentation about :ref:`the 500 (HTTP Internal Server Error) view
  95. <http_internal_server_error_view>` for more information.