2
0

urls.txt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. from django.conf.urls import include, url
  19. urlpatterns = [
  20. url(r'^index/$', index_view, name='main-view'),
  21. url(r'^weblog/', include('blog.urls')),
  22. ...
  23. ]
  24. The ``regex`` parameter should be a string or
  25. :func:`~django.utils.translation.gettext_lazy()` (see
  26. :ref:`translating-urlpatterns`) that contains a regular expression compatible
  27. with Python's :py:mod:`re` module. Strings typically use raw string syntax
  28. (``r''``) so that they can contain sequences like ``\d`` without the need to
  29. escape the backslash with another backslash.
  30. The ``view`` parameter is a view function or the result of
  31. :meth:`~django.views.generic.base.View.as_view` for class-based views. It can
  32. also be an :func:`include`.
  33. The ``kwargs`` parameter allows you to pass additional arguments to the view
  34. function or method. See :ref:`views-extra-options` for an example.
  35. See :ref:`Naming URL patterns <naming-url-patterns>` for why the ``name``
  36. parameter is useful.
  37. ``include()``
  38. =============
  39. .. function:: include(module, namespace=None)
  40. include(pattern_list)
  41. include((pattern_list, app_namespace), namespace=None)
  42. A function that takes a full Python import path to another URLconf module
  43. that should be "included" in this place. Optionally, the :term:`application
  44. namespace` and :term:`instance namespace` where the entries will be included
  45. into can also be specified.
  46. Usually, the application namespace should be specified by the included
  47. module. If an application namespace is set, the ``namespace`` argument
  48. can be used to set a different instance namespace.
  49. ``include()`` also accepts as an argument either an iterable that returns
  50. URL patterns or a 2-tuple containing such iterable plus the names of the
  51. application namespaces.
  52. :arg module: URLconf module (or module name)
  53. :arg namespace: Instance namespace for the URL entries being included
  54. :type namespace: string
  55. :arg pattern_list: Iterable of :func:`django.conf.urls.url` instances
  56. :arg app_namespace: Application namespace for the URL entries being included
  57. :type app_namespace: string
  58. :arg instance_namespace: Instance namespace for the URL entries being included
  59. :type instance_namespace: string
  60. See :ref:`including-other-urlconfs` and :ref:`namespaces-and-include`.
  61. ``handler400``
  62. ==============
  63. .. data:: handler400
  64. A callable, or a string representing the full Python import path to the view
  65. that should be called if the HTTP client has sent a request that caused an error
  66. condition and a response with a status code of 400.
  67. By default, this is :func:`django.views.defaults.bad_request`. If you
  68. implement a custom view, be sure it returns an
  69. :class:`~django.http.HttpResponseBadRequest`.
  70. ``handler403``
  71. ==============
  72. .. data:: handler403
  73. A callable, or a string representing the full Python import path to the view
  74. that should be called if the user doesn't have the permissions required to
  75. access a resource.
  76. By default, this is :func:`django.views.defaults.permission_denied`. If you
  77. implement a custom view, be sure it returns an
  78. :class:`~django.http.HttpResponseForbidden`.
  79. ``handler404``
  80. ==============
  81. .. data:: handler404
  82. A callable, or a string representing the full Python import path to the view
  83. that should be called if none of the URL patterns match.
  84. By default, this is :func:`django.views.defaults.page_not_found`. If you
  85. implement a custom view, be sure it returns an
  86. :class:`~django.http.HttpResponseNotFound`.
  87. ``handler500``
  88. ==============
  89. .. data:: handler500
  90. A callable, or a string representing the full Python import path to the view
  91. that should be called in case of server errors. Server errors happen when you
  92. have runtime errors in view code.
  93. By default, this is :func:`django.views.defaults.server_error`. If you
  94. implement a custom view, be sure it returns an
  95. :class:`~django.http.HttpResponseServerError`.