decorators.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. .. module:: django.views.decorators.vary
  69. Vary headers
  70. ============
  71. The decorators in :mod:`django.views.decorators.vary` can be used to control
  72. caching based on specific request headers.
  73. .. function:: vary_on_cookie(func)
  74. .. function:: vary_on_headers(*headers)
  75. The ``Vary`` header defines which request headers a cache mechanism should take
  76. into account when building its cache key.
  77. See :ref:`using vary headers <using-vary-headers>`.
  78. .. module:: django.views.decorators.cache
  79. Caching
  80. =======
  81. The decorators in :mod:`django.views.decorators.cache` control server and
  82. client-side caching.
  83. .. function:: cache_control(**kwargs)
  84. This decorator patches the response's ``Cache-Control`` header by adding
  85. all of the keyword arguments to it. See
  86. :func:`~django.utils.cache.patch_cache_control` for the details of the
  87. transformation.
  88. .. versionchanged:: 5.0
  89. Support for wrapping asynchronous view functions was added.
  90. .. function:: never_cache(view_func)
  91. This decorator adds an ``Expires`` header to the current date/time.
  92. This decorator adds a ``Cache-Control: max-age=0, no-cache, no-store,
  93. must-revalidate, private`` header to a response to indicate that a page
  94. should never be cached.
  95. Each header is only added if it isn't already set.
  96. .. versionchanged:: 5.0
  97. Support for wrapping asynchronous view functions was added.
  98. .. module:: django.views.decorators.common
  99. Common
  100. ======
  101. The decorators in :mod:`django.views.decorators.common` allow per-view
  102. customization of :class:`~django.middleware.common.CommonMiddleware` behavior.
  103. .. function:: no_append_slash()
  104. This decorator allows individual views to be excluded from
  105. :setting:`APPEND_SLASH` URL normalization.
  106. .. versionchanged:: 5.0
  107. Support for wrapping asynchronous view functions was added.