decorators.txt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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:: conditional_page()
  45. This decorator provides the conditional GET operation handling of
  46. :class:`~django.middleware.http.ConditionalGetMiddleware` to a view.
  47. .. function:: etag(etag_func)
  48. .. function:: last_modified(last_modified_func)
  49. These decorators can be used to generate ``ETag`` and ``Last-Modified``
  50. headers; see
  51. :doc:`conditional view processing </topics/conditional-view-processing>`.
  52. .. module:: django.views.decorators.gzip
  53. GZip compression
  54. ================
  55. The decorators in :mod:`django.views.decorators.gzip` control content
  56. compression on a per-view basis.
  57. .. function:: gzip_page()
  58. This decorator compresses content if the browser allows gzip compression.
  59. It sets the ``Vary`` header accordingly, so that caches will base their
  60. storage on the ``Accept-Encoding`` header.
  61. .. module:: django.views.decorators.vary
  62. Vary headers
  63. ============
  64. The decorators in :mod:`django.views.decorators.vary` can be used to control
  65. caching based on specific request headers.
  66. .. function:: vary_on_cookie(func)
  67. .. function:: vary_on_headers(*headers)
  68. The ``Vary`` header defines which request headers a cache mechanism should take
  69. into account when building its cache key.
  70. See :ref:`using vary headers <using-vary-headers>`.
  71. .. module:: django.views.decorators.cache
  72. Caching
  73. =======
  74. The decorators in :mod:`django.views.decorators.cache` control server and
  75. client-side caching.
  76. .. function:: cache_control(**kwargs)
  77. This decorator patches the response's ``Cache-Control`` header by adding
  78. all of the keyword arguments to it. See
  79. :func:`~django.utils.cache.patch_cache_control` for the details of the
  80. transformation.
  81. .. function:: never_cache(view_func)
  82. This decorator adds an ``Expires`` header to the current date/time.
  83. This decorator adds a ``Cache-Control: max-age=0, no-cache, no-store,
  84. must-revalidate, private`` header to a response to indicate that a page
  85. should never be cached.
  86. Each header is only added if it isn't already set.
  87. .. module:: django.views.decorators.common
  88. Common
  89. ======
  90. The decorators in :mod:`django.views.decorators.common` allow per-view
  91. customization of :class:`~django.middleware.common.CommonMiddleware` behavior.
  92. .. function:: no_append_slash()
  93. This decorator allows individual views to be excluded from
  94. :setting:`APPEND_SLASH` URL normalization.