| | 1 | =============== |
| | 2 | View Decorators |
| | 3 | =============== |
| | 4 | |
| | 5 | .. currentmodule:: django.views.decorators.http |
| | 6 | |
| | 7 | Django provides several decorators that can be applied to views to support |
| | 8 | various HTTP features. |
| | 9 | |
| | 10 | Allowed HTTP Methods |
| | 11 | ==================== |
| | 12 | |
| | 13 | .. function:: require_http_methods(request_method_list) |
| | 14 | |
| | 15 | This decorator is used to make a view only accept particular request methods. |
| | 16 | Usage:: |
| | 17 | |
| | 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 | |
| | 23 | Note that request methods should be in uppercase. |
| | 24 | |
| | 25 | .. function:: require_GET |
| | 26 | |
| | 27 | Decorator to require that a view only accept the GET method. |
| | 28 | |
| | 29 | .. function:: require_POST |
| | 30 | |
| | 31 | Decorator to require that a view only accept the POST method. |
| | 32 | |
| | 33 | Conditional view processing |
| | 34 | =========================== |
| | 35 | |
| | 36 | .. function:: condition(etag_func=None, last_modified_func=None) |
| | 37 | |
| | 38 | .. function:: etag(etag_func) |
| | 39 | |
| | 40 | .. function:: last_modified(last_modified_func) |
| | 41 | |
| | 42 | These decorators can be used to generate ``ETag`` and ``Last-Modified`` |
| | 43 | headers; see |
| | 44 | :doc:`conditional view processing </topics/conditional-view-processing>`. |
| | 45 | |
| | 46 | .. currentmodule:: django.views.decorators.http |
| | 47 | |
| | 48 | GZip Compression |
| | 49 | ================ |
| | 50 | |
| | 51 | .. function:: gzip_page() |
| | 52 | |
| | 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 | |
| | 57 | .. currentmodule:: django.views.decorators.vary |
| | 58 | |
| | 59 | Vary Headers |
| | 60 | ============ |
| | 61 | |
| | 62 | The ``Vary`` header defines which request headers a cache mechanism should take |
| | 63 | into account when building its cache key. |
| | 64 | |
| | 65 | .. function:: vary_on_cookie(func) |
| | 66 | |
| | 67 | .. function:: vary_on_headers(*headers) |
| | 68 | |
| | 69 | See :ref:`using vary headers <using-vary-headers>`. |