decorators.txt 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. Allowed HTTP methods
  8. ====================
  9. The decorators in :mod:`django.views.decorators.http` can be used to restrict
  10. access to views based on the request method. These decorators will return
  11. a :class:`django.http.HttpResponseNotAllowed` if the conditions are not met.
  12. .. function:: require_http_methods(request_method_list)
  13. Decorator to require that a view only accept particular request
  14. methods. Usage::
  15. from django.views.decorators.http import require_http_methods
  16. @require_http_methods(["GET", "POST"])
  17. def my_view(request):
  18. # I can assume now that only GET or POST requests make it this far
  19. # ...
  20. pass
  21. Note that request methods should be in uppercase.
  22. .. function:: require_GET()
  23. Decorator to require that a view only accept the GET method.
  24. .. function:: require_POST()
  25. Decorator to require that a view only accept the POST method.
  26. .. function:: require_safe()
  27. Decorator to require that a view only accept the GET and HEAD methods.
  28. These methods are commonly considered "safe" because they should not have
  29. the significance of taking an action other than retrieving the requested
  30. resource.
  31. .. note::
  32. Django will automatically strip the content of responses to HEAD
  33. requests while leaving the headers unchanged, so you may handle HEAD
  34. requests exactly like GET requests in your views. Since some software,
  35. such as link checkers, rely on HEAD requests, you might prefer
  36. using ``require_safe`` instead of ``require_GET``.
  37. Conditional view processing
  38. ===========================
  39. The following decorators in :mod:`django.views.decorators.http` can be used to
  40. control caching behavior on particular views.
  41. .. function:: condition(etag_func=None, last_modified_func=None)
  42. .. function:: etag(etag_func)
  43. .. function:: last_modified(last_modified_func)
  44. These decorators can be used to generate ``ETag`` and ``Last-Modified``
  45. headers; see
  46. :doc:`conditional view processing </topics/conditional-view-processing>`.
  47. .. module:: django.views.decorators.gzip
  48. GZip compression
  49. ================
  50. The decorators in :mod:`django.views.decorators.gzip` control content
  51. compression on a per-view basis.
  52. .. function:: gzip_page()
  53. This decorator compresses content if the browser allows gzip compression.
  54. It sets the ``Vary`` header accordingly, so that caches will base their
  55. storage on the ``Accept-Encoding`` header.
  56. .. module:: django.views.decorators.vary
  57. Vary headers
  58. ============
  59. The decorators in :mod:`django.views.decorators.vary` can be used to control
  60. caching based on specific request headers.
  61. .. function:: vary_on_cookie(func)
  62. .. function:: vary_on_headers(*headers)
  63. The ``Vary`` header defines which request headers a cache mechanism should take
  64. into account when building its cache key.
  65. See :ref:`using vary headers <using-vary-headers>`.
  66. .. module:: django.views.decorators.cache
  67. Caching
  68. =======
  69. The decorators in :mod:`django.views.decorators.cache` control server and
  70. client-side caching.
  71. .. function:: never_cache(view_func)
  72. This decorator adds a ``Cache-Control: max-age=0, no-cache, no-store,
  73. must-revalidate`` header to a response to indicate that a page should never
  74. be cached.
  75. .. versionchanged:: 1.9
  76. Before Django 1.9, ``Cache-Control: max-age=0`` was sent. This didn't
  77. reliably prevent caching in all browsers.