urls.txt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. static()
  35. --------
  36. .. function:: static.static(prefix, view='django.views.static.serve', **kwargs)
  37. Helper function to return a URL pattern for serving files in debug mode::
  38. from django.conf import settings
  39. from django.conf.urls.static import static
  40. urlpatterns = patterns('',
  41. # ... the rest of your URLconf goes here ...
  42. ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  43. url()
  44. -----
  45. .. function:: url(regex, view, kwargs=None, name=None, prefix='')
  46. You can use the ``url()`` function, instead of a tuple, as an argument to
  47. ``patterns()``. This is convenient if you want to specify a name without the
  48. optional extra arguments dictionary. For example::
  49. urlpatterns = patterns('',
  50. url(r'^index/$', index_view, name="main-view"),
  51. ...
  52. )
  53. This function takes five arguments, most of which are optional::
  54. url(regex, view, kwargs=None, name=None, prefix='')
  55. See :ref:`Naming URL patterns <naming-url-patterns>` for why the ``name``
  56. parameter is useful.
  57. The ``prefix`` parameter has the same meaning as the first argument to
  58. ``patterns()`` and is only relevant when you're passing a string as the
  59. ``view`` parameter.
  60. include()
  61. ---------
  62. .. function:: include(module[, namespace=None, app_name=None])
  63. include(pattern_list)
  64. include((pattern_list, app_namespace, instance_namespace))
  65. A function that takes a full Python import path to another URLconf module
  66. that should be "included" in this place. Optionally, the :term:`application
  67. namespace` and :term:`instance namespace` where the entries will be included
  68. into can also be specified.
  69. ``include()`` also accepts as an argument either an iterable that returns
  70. URL patterns or a 3-tuple containing such iterable plus the names of the
  71. application and instance namespaces.
  72. :arg module: URLconf module (or module name)
  73. :arg namespace: Instance namespace for the URL entries being included
  74. :type namespace: string
  75. :arg app_name: Application namespace for the URL entries being included
  76. :type app_name: string
  77. :arg pattern_list: Iterable of URL entries as returned by :func:`patterns`
  78. :arg app_namespace: Application namespace for the URL entries being included
  79. :type app_namespace: string
  80. :arg instance_namespace: Instance namespace for the URL entries being included
  81. :type instance_namespace: string
  82. See :ref:`including-other-urlconfs` and :ref:`namespaces-and-include`.
  83. handler403
  84. ----------
  85. .. data:: handler403
  86. A callable, or a string representing the full Python import path to the view
  87. that should be called if the user doesn't have the permissions required to
  88. access a resource.
  89. By default, this is ``'django.views.defaults.permission_denied'``. That default
  90. value should suffice.
  91. See the documentation about :ref:`the 403 (HTTP Forbidden) view
  92. <http_forbidden_view>` for more information.
  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.