urls.txt 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. url()
  15. =====
  16. .. function:: url(regex, view, kwargs=None, name=None)
  17. ``urlpatterns`` should be a list of ``url()`` instances. For example::
  18. urlpatterns = [
  19. url(r'^index/$', index_view, name="main-view"),
  20. ...
  21. ]
  22. The ``kwargs`` parameter allows you to pass additional arguments to the view
  23. function or method. See :ref:`views-extra-options` for an example.
  24. See :ref:`Naming URL patterns <naming-url-patterns>` for why the ``name``
  25. parameter is useful.
  26. include()
  27. =========
  28. .. function:: include(module, namespace=None, app_name=None)
  29. include(pattern_list)
  30. include((pattern_list, app_namespace), namespace=None)
  31. include((pattern_list, app_namespace, instance_namespace))
  32. A function that takes a full Python import path to another URLconf module
  33. that should be "included" in this place. Optionally, the :term:`application
  34. namespace` and :term:`instance namespace` where the entries will be included
  35. into can also be specified.
  36. Usually, the application namespace should be specified by the included
  37. module. If an application namespace is set, the ``namespace`` argument
  38. can be used to set a different instance namespace.
  39. ``include()`` also accepts as an argument either an iterable that returns
  40. URL patterns, a 2-tuple containing such iterable plus the names of the
  41. application namespaces, or a 3-tuple containing the iterable and the names
  42. of both the application and instance namespace.
  43. :arg module: URLconf module (or module name)
  44. :arg namespace: Instance namespace for the URL entries being included
  45. :type namespace: string
  46. :arg app_name: Application namespace for the URL entries being included
  47. :type app_name: string
  48. :arg pattern_list: Iterable of :func:`django.conf.urls.url` instances
  49. :arg app_namespace: Application namespace for the URL entries being included
  50. :type app_namespace: string
  51. :arg instance_namespace: Instance namespace for the URL entries being included
  52. :type instance_namespace: string
  53. See :ref:`including-other-urlconfs` and :ref:`namespaces-and-include`.
  54. .. deprecated:: 1.9
  55. Support for the ``app_name`` argument is deprecated and will be removed in
  56. Django 2.0. Specify the ``app_name`` as explained in
  57. :ref:`namespaces-and-include` instead.
  58. Support for passing a 3-tuple is also deprecated and will be removed in
  59. Django 2.0. Pass a 2-tuple containing the pattern list and application
  60. namespace, and use the ``namespace`` argument instead.
  61. Lastly, support for an instance namespace without an application namespace
  62. has been deprecated and will be removed in Django 2.0. Specify the
  63. application namespace or remove the instance namespace.
  64. handler400
  65. ==========
  66. .. data:: handler400
  67. A callable, or a string representing the full Python import path to the view
  68. that should be called if the HTTP client has sent a request that caused an error
  69. condition and a response with a status code of 400.
  70. By default, this is ``'django.views.defaults.bad_request'``. If you
  71. implement a custom view, be sure it returns an
  72. :class:`~django.http.HttpResponseBadRequest`.
  73. See the documentation about :ref:`the 400 (bad request) view
  74. <http_bad_request_view>` for more information.
  75. handler403
  76. ==========
  77. .. data:: handler403
  78. A callable, or a string representing the full Python import path to the view
  79. that should be called if the user doesn't have the permissions required to
  80. access a resource.
  81. By default, this is ``'django.views.defaults.permission_denied'``. If you
  82. implement a custom view, be sure it returns an
  83. :class:`~django.http.HttpResponseForbidden`.
  84. See the documentation about :ref:`the 403 (HTTP Forbidden) view
  85. <http_forbidden_view>` for more information.
  86. handler404
  87. ==========
  88. .. data:: handler404
  89. A callable, or a string representing the full Python import path to the view
  90. that should be called if none of the URL patterns match.
  91. By default, this is ``'django.views.defaults.page_not_found'``. If you
  92. implement a custom view, be sure it returns an
  93. :class:`~django.http.HttpResponseNotFound`.
  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'``. If you
  103. implement a custom view, be sure it returns an
  104. :class:`~django.http.HttpResponseServerError`.
  105. See the documentation about :ref:`the 500 (HTTP Internal Server Error) view
  106. <http_internal_server_error_view>` for more information.