urls.txt 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. ======================================
  2. ``django.conf.urls`` utility functions
  3. ======================================
  4. .. module:: django.conf.urls
  5. static()
  6. --------
  7. .. function:: static.static(prefix, view=django.views.static.serve, **kwargs)
  8. Helper function to return a URL pattern for serving files in debug mode::
  9. from django.conf import settings
  10. from django.conf.urls.static import static
  11. urlpatterns = [
  12. # ... the rest of your URLconf goes here ...
  13. ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  14. .. versionchanged:: 1.8
  15. The ``view`` argument changed from a string
  16. (``'django.views.static.serve'``) to the function.
  17. url()
  18. -----
  19. .. function:: url(regex, view, kwargs=None, name=None)
  20. ``urlpatterns`` should be a list of ``url()`` instances. For example::
  21. urlpatterns = [
  22. url(r'^index/$', index_view, name="main-view"),
  23. ...
  24. ]
  25. The ``kwargs`` parameter allows you to pass additional arguments to the view
  26. function or method. See :ref:`views-extra-options` for an example.
  27. See :ref:`Naming URL patterns <naming-url-patterns>` for why the ``name``
  28. parameter is useful.
  29. include()
  30. ---------
  31. .. function:: include(module, namespace=None, app_name=None)
  32. include(pattern_list)
  33. include((pattern_list, app_namespace), namespace=None)
  34. include((pattern_list, app_namespace, instance_namespace))
  35. A function that takes a full Python import path to another URLconf module
  36. that should be "included" in this place. Optionally, the :term:`application
  37. namespace` and :term:`instance namespace` where the entries will be included
  38. into can also be specified.
  39. Usually, the application namespace should be specified by the included
  40. module. If an application namespace is set, the ``namespace`` argument
  41. can be used to set a different instance namespace.
  42. ``include()`` also accepts as an argument either an iterable that returns
  43. URL patterns, a 2-tuple containing such iterable plus the names of the
  44. application namespaces, or a 3-tuple containing the iterable and the names
  45. of both the application and instance namespace.
  46. :arg module: URLconf module (or module name)
  47. :arg namespace: Instance namespace for the URL entries being included
  48. :type namespace: string
  49. :arg app_name: Application namespace for the URL entries being included
  50. :type app_name: string
  51. :arg pattern_list: Iterable of :func:`django.conf.urls.url` instances
  52. :arg app_namespace: Application namespace for the URL entries being included
  53. :type app_namespace: string
  54. :arg instance_namespace: Instance namespace for the URL entries being included
  55. :type instance_namespace: string
  56. See :ref:`including-other-urlconfs` and :ref:`namespaces-and-include`.
  57. .. deprecated:: 1.9
  58. Support for the ``app_name`` argument is deprecated and will be removed in
  59. Django 2.0. Specify the ``app_name`` as explained in
  60. :ref:`namespaces-and-include` instead.
  61. Support for passing a 3-tuple is also deprecated and will be removed in
  62. Django 2.0. Pass a 2-tuple containing the pattern list and application
  63. namespace, and use the ``namespace`` argument instead.
  64. Lastly, support for an instance namespace without an application namespace
  65. has been deprecated and will be removed in Django 2.0. Specify the
  66. application namespace or remove the instance namespace.
  67. handler400
  68. ----------
  69. .. data:: handler400
  70. A callable, or a string representing the full Python import path to the view
  71. that should be called if the HTTP client has sent a request that caused an error
  72. condition and a response with a status code of 400.
  73. By default, this is ``'django.views.defaults.bad_request'``. That default
  74. value should suffice.
  75. See the documentation about :ref:`the 400 (bad request) view
  76. <http_bad_request_view>` for more information.
  77. handler403
  78. ----------
  79. .. data:: handler403
  80. A callable, or a string representing the full Python import path to the view
  81. that should be called if the user doesn't have the permissions required to
  82. access a resource.
  83. By default, this is ``'django.views.defaults.permission_denied'``. That default
  84. value should suffice.
  85. See the documentation about :ref:`the 403 (HTTP Forbidden) view
  86. <http_forbidden_view>` for more information.
  87. handler404
  88. ----------
  89. .. data:: handler404
  90. A callable, or a string representing the full Python import path to the view
  91. that should be called if none of the URL patterns match.
  92. By default, this is ``'django.views.defaults.page_not_found'``. That default
  93. value should suffice.
  94. See the documentation about :ref:`the 404 (HTTP Not Found) view
  95. <http_not_found_view>` for more information.
  96. handler500
  97. ----------
  98. .. data:: handler500
  99. A callable, or a string representing the full Python import path to the view
  100. that should be called in case of server errors. Server errors happen when you
  101. have runtime errors in view code.
  102. By default, this is ``'django.views.defaults.server_error'``. That default
  103. value should suffice.
  104. See the documentation about :ref:`the 500 (HTTP Internal Server Error) view
  105. <http_internal_server_error_view>` for more information.