decorators.txt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. .. function:: require_GET()
  25. Decorator to require that a view only accepts the GET method.
  26. .. function:: require_POST()
  27. Decorator to require that a view only accepts the POST method.
  28. .. function:: require_safe()
  29. Decorator to require that a view only accepts the GET and HEAD methods.
  30. These methods are commonly considered "safe" because they should not have
  31. the significance of taking an action other than retrieving the requested
  32. resource.
  33. .. note::
  34. Web servers should automatically strip the content of responses to HEAD
  35. requests while leaving the headers unchanged, so you may handle HEAD
  36. requests exactly like GET requests in your views. Since some software,
  37. such as link checkers, rely on HEAD requests, you might prefer
  38. using ``require_safe`` instead of ``require_GET``.
  39. Conditional view processing
  40. ===========================
  41. The following decorators in :mod:`django.views.decorators.http` can be used to
  42. control caching behavior on particular views.
  43. .. function:: condition(etag_func=None, last_modified_func=None)
  44. .. function:: etag(etag_func)
  45. .. function:: last_modified(last_modified_func)
  46. These decorators can be used to generate ``ETag`` and ``Last-Modified``
  47. headers; see
  48. :doc:`conditional view processing </topics/conditional-view-processing>`.
  49. .. module:: django.views.decorators.gzip
  50. GZip compression
  51. ================
  52. The decorators in :mod:`django.views.decorators.gzip` control content
  53. compression on a per-view basis.
  54. .. function:: gzip_page()
  55. This decorator compresses content if the browser allows gzip compression.
  56. It sets the ``Vary`` header accordingly, so that caches will base their
  57. storage on the ``Accept-Encoding`` header.
  58. .. module:: django.views.decorators.vary
  59. Vary headers
  60. ============
  61. The decorators in :mod:`django.views.decorators.vary` can be used to control
  62. caching based on specific request headers.
  63. .. function:: vary_on_cookie(func)
  64. .. function:: vary_on_headers(*headers)
  65. The ``Vary`` header defines which request headers a cache mechanism should take
  66. into account when building its cache key.
  67. See :ref:`using vary headers <using-vary-headers>`.
  68. .. module:: django.views.decorators.cache
  69. Caching
  70. =======
  71. The decorators in :mod:`django.views.decorators.cache` control server and
  72. client-side caching.
  73. .. function:: cache_control(**kwargs)
  74. This decorator patches the response's ``Cache-Control`` header by adding
  75. all of the keyword arguments to it. See
  76. :func:`~django.utils.cache.patch_cache_control` for the details of the
  77. transformation.
  78. .. versionchanged:: 5.0
  79. Support for wrapping asynchronous view functions was added.
  80. .. function:: never_cache(view_func)
  81. This decorator adds an ``Expires`` header to the current date/time.
  82. This decorator adds a ``Cache-Control: max-age=0, no-cache, no-store,
  83. must-revalidate, private`` header to a response to indicate that a page
  84. should never be cached.
  85. Each header is only added if it isn't already set.
  86. .. versionchanged:: 5.0
  87. Support for wrapping asynchronous view functions was added.
  88. .. module:: django.views.decorators.common
  89. Common
  90. ======
  91. The decorators in :mod:`django.views.decorators.common` allow per-view
  92. customization of :class:`~django.middleware.common.CommonMiddleware` behavior.
  93. .. function:: no_append_slash()
  94. This decorator allows individual views to be excluded from
  95. :setting:`APPEND_SLASH` URL normalization.