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