decorators.txt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. ===============
  2. View decorators
  3. ===============
  4. .. module:: django.views.decorators.http
  5. Django provides several decorators that can be applied to views to support
  6. various HTTP features.
  7. See :ref:`decorating-class-based-views` for how to use these decorators with
  8. class-based views.
  9. Allowed HTTP methods
  10. ====================
  11. The decorators in :mod:`django.views.decorators.http` can be used to restrict
  12. access to views based on the request method. These decorators will return
  13. a :class:`django.http.HttpResponseNotAllowed` if the conditions are not met.
  14. .. function:: require_http_methods(request_method_list)
  15. Decorator to require that a view only accepts particular request
  16. methods. Usage::
  17. from django.views.decorators.http import require_http_methods
  18. @require_http_methods(["GET", "POST"])
  19. def my_view(request):
  20. # I can assume now that only GET or POST requests make it this far
  21. # ...
  22. pass
  23. Note that request methods should be in uppercase.
  24. .. versionchanged:: 5.0
  25. Support for wrapping asynchronous view functions was added.
  26. .. function:: require_GET()
  27. Decorator to require that a view only accepts the GET method.
  28. .. versionchanged:: 5.0
  29. Support for wrapping asynchronous view functions was added.
  30. .. function:: require_POST()
  31. Decorator to require that a view only accepts the POST method.
  32. .. versionchanged:: 5.0
  33. Support for wrapping asynchronous view functions was added.
  34. .. function:: require_safe()
  35. Decorator to require that a view only accepts the GET and HEAD methods.
  36. These methods are commonly considered "safe" because they should not have
  37. the significance of taking an action other than retrieving the requested
  38. resource.
  39. .. note::
  40. Web servers should automatically strip the content of responses to HEAD
  41. requests while leaving the headers unchanged, so you may handle HEAD
  42. requests exactly like GET requests in your views. Since some software,
  43. such as link checkers, rely on HEAD requests, you might prefer
  44. using ``require_safe`` instead of ``require_GET``.
  45. .. versionchanged:: 5.0
  46. Support for wrapping asynchronous view functions was added.
  47. Conditional view processing
  48. ===========================
  49. The following decorators in :mod:`django.views.decorators.http` can be used to
  50. control caching behavior on particular views.
  51. .. function:: condition(etag_func=None, last_modified_func=None)
  52. .. function:: etag(etag_func)
  53. .. function:: last_modified(last_modified_func)
  54. These decorators can be used to generate ``ETag`` and ``Last-Modified``
  55. headers; see
  56. :doc:`conditional view processing </topics/conditional-view-processing>`.
  57. .. versionchanged:: 5.0
  58. Support for wrapping asynchronous view functions was added.
  59. .. module:: django.views.decorators.gzip
  60. GZip compression
  61. ================
  62. The decorators in :mod:`django.views.decorators.gzip` control content
  63. compression on a per-view basis.
  64. .. function:: gzip_page()
  65. This decorator compresses content if the browser allows gzip compression.
  66. It sets the ``Vary`` header accordingly, so that caches will base their
  67. storage on the ``Accept-Encoding`` header.
  68. .. versionchanged:: 5.0
  69. Support for wrapping asynchronous view functions was added.
  70. .. module:: django.views.decorators.vary
  71. Vary headers
  72. ============
  73. The decorators in :mod:`django.views.decorators.vary` can be used to control
  74. caching based on specific request headers.
  75. .. function:: vary_on_cookie(func)
  76. .. versionchanged:: 5.0
  77. Support for wrapping asynchronous view functions was added.
  78. .. function:: vary_on_headers(*headers)
  79. The ``Vary`` header defines which request headers a cache mechanism should take
  80. into account when building its cache key.
  81. See :ref:`using vary headers <using-vary-headers>`.
  82. .. versionchanged:: 5.0
  83. Support for wrapping asynchronous view functions was added.
  84. .. module:: django.views.decorators.cache
  85. Caching
  86. =======
  87. The decorators in :mod:`django.views.decorators.cache` control server and
  88. client-side caching.
  89. .. function:: cache_control(**kwargs)
  90. This decorator patches the response's ``Cache-Control`` header by adding
  91. all of the keyword arguments to it. See
  92. :func:`~django.utils.cache.patch_cache_control` for the details of the
  93. transformation.
  94. .. versionchanged:: 5.0
  95. Support for wrapping asynchronous view functions was added.
  96. .. function:: never_cache(view_func)
  97. This decorator adds an ``Expires`` header to the current date/time.
  98. This decorator adds a ``Cache-Control: max-age=0, no-cache, no-store,
  99. must-revalidate, private`` header to a response to indicate that a page
  100. should never be cached.
  101. Each header is only added if it isn't already set.
  102. .. versionchanged:: 5.0
  103. Support for wrapping asynchronous view functions was added.
  104. .. module:: django.views.decorators.common
  105. Common
  106. ======
  107. The decorators in :mod:`django.views.decorators.common` allow per-view
  108. customization of :class:`~django.middleware.common.CommonMiddleware` behavior.
  109. .. function:: no_append_slash()
  110. This decorator allows individual views to be excluded from
  111. :setting:`APPEND_SLASH` URL normalization.
  112. .. versionchanged:: 5.0
  113. Support for wrapping asynchronous view functions was added.