decorators.txt 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. ===============
  2. View Decorators
  3. ===============
  4. .. currentmodule:: 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. .. function:: require_http_methods(request_method_list)
  10. This decorator is used to make a view only accept particular request methods.
  11. Usage::
  12. from django.views.decorators.http import require_http_methods
  13. @require_http_methods(["GET", "POST"])
  14. def my_view(request):
  15. # I can assume now that only GET or POST requests make it this far
  16. # ...
  17. pass
  18. Note that request methods should be in uppercase.
  19. .. function:: require_GET()
  20. Decorator to require that a view only accept the GET method.
  21. .. function:: require_POST()
  22. Decorator to require that a view only accept the POST method.
  23. Conditional view processing
  24. ===========================
  25. .. function:: condition(etag_func=None, last_modified_func=None)
  26. .. function:: etag(etag_func)
  27. .. function:: last_modified(last_modified_func)
  28. These decorators can be used to generate ``ETag`` and ``Last-Modified``
  29. headers; see
  30. :doc:`conditional view processing </topics/conditional-view-processing>`.
  31. .. currentmodule:: django.views.decorators.http
  32. GZip Compression
  33. ================
  34. .. function:: gzip_page()
  35. This decorator compresses content if the browser allows gzip compression.
  36. It sets the ``Vary`` header accordingly, so that caches will base their
  37. storage on the ``Accept-Encoding`` header.
  38. .. currentmodule:: django.views.decorators.vary
  39. Vary Headers
  40. ============
  41. The ``Vary`` header defines which request headers a cache mechanism should take
  42. into account when building its cache key.
  43. .. function:: vary_on_cookie(func)
  44. .. function:: vary_on_headers(*headers)
  45. See :ref:`using vary headers <using-vary-headers>`.