urls.txt 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. The ``kwargs`` parameter allows you to pass additional arguments to the view
  56. function or method. See :ref:`views-extra-options` for an example.
  57. See :ref:`Naming URL patterns <naming-url-patterns>` for why the ``name``
  58. parameter is useful.
  59. The ``prefix`` parameter has the same meaning as the first argument to
  60. ``patterns()`` and is only relevant when you're passing a string as the
  61. ``view`` parameter.
  62. include()
  63. ---------
  64. .. function:: include(module[, namespace=None, app_name=None])
  65. include(pattern_list)
  66. include((pattern_list, app_namespace, instance_namespace))
  67. A function that takes a full Python import path to another URLconf module
  68. that should be "included" in this place. Optionally, the :term:`application
  69. namespace` and :term:`instance namespace` where the entries will be included
  70. into can also be specified.
  71. ``include()`` also accepts as an argument either an iterable that returns
  72. URL patterns or a 3-tuple containing such iterable plus the names of the
  73. application and instance namespaces.
  74. :arg module: URLconf module (or module name)
  75. :arg namespace: Instance namespace for the URL entries being included
  76. :type namespace: string
  77. :arg app_name: Application namespace for the URL entries being included
  78. :type app_name: string
  79. :arg pattern_list: Iterable of URL entries as returned by :func:`patterns`
  80. :arg app_namespace: Application namespace for the URL entries being included
  81. :type app_namespace: string
  82. :arg instance_namespace: Instance namespace for the URL entries being included
  83. :type instance_namespace: string
  84. See :ref:`including-other-urlconfs` and :ref:`namespaces-and-include`.
  85. handler400
  86. ----------
  87. .. data:: handler400
  88. A callable, or a string representing the full Python import path to the view
  89. that should be called if the HTTP client has sent a request that caused an error
  90. condition and a response with a status code of 400.
  91. By default, this is ``'django.views.defaults.bad_request'``. That default
  92. value should suffice.
  93. See the documentation about :ref:`the 400 (bad request) view
  94. <http_bad_request_view>` for more information.
  95. handler403
  96. ----------
  97. .. data:: handler403
  98. A callable, or a string representing the full Python import path to the view
  99. that should be called if the user doesn't have the permissions required to
  100. access a resource.
  101. By default, this is ``'django.views.defaults.permission_denied'``. That default
  102. value should suffice.
  103. See the documentation about :ref:`the 403 (HTTP Forbidden) view
  104. <http_forbidden_view>` for more information.
  105. handler404
  106. ----------
  107. .. data:: handler404
  108. A callable, or a string representing the full Python import path to the view
  109. that should be called if none of the URL patterns match.
  110. By default, this is ``'django.views.defaults.page_not_found'``. That default
  111. value should suffice.
  112. See the documentation about :ref:`the 404 (HTTP Not Found) view
  113. <http_not_found_view>` for more information.
  114. handler500
  115. ----------
  116. .. data:: handler500
  117. A callable, or a string representing the full Python import path to the view
  118. that should be called in case of server errors. Server errors happen when you
  119. have runtime errors in view code.
  120. By default, this is ``'django.views.defaults.server_error'``. That default
  121. value should suffice.
  122. See the documentation about :ref:`the 500 (HTTP Internal Server Error) view
  123. <http_internal_server_error_view>` for more information.