File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -59,6 +59,31 @@ class PostView(APIView):
5959 return Response(content)
6060```
6161
62+
63+ ## Using cache with @api_view decorator
64+
65+ When using @api_view decorator, the Django-provided method-based cache decorators such as [ ` cache_page ` ] [ page ] ,
66+ [ ` vary_on_cookie ` ] [ cookie ] and [ ` vary_on_headers ` ] [ headers ] can be called directly.
67+
68+ ``` python
69+ from django.views.decorators.cache import cache_page
70+ from django.views.decorators.vary import vary_on_cookie
71+
72+ from rest_framework.decorators import api_view
73+ from rest_framework.response import Response
74+
75+
76+ @cache_page (60 * 15 )
77+ @vary_on_cookie
78+ @api_view ([' GET' ])
79+ def get_user_list (request ):
80+ content = {
81+ ' user_feed' : request.user.get_user_feed()
82+ }
83+ return Response(content)
84+ ```
85+
86+
6287** NOTE:** The [ ` cache_page ` ] [ page ] decorator only caches the
6388` GET ` and ` HEAD ` responses with status 200.
6489
You can’t perform that action at this time.
0 commit comments