Reputation: 12524
How can I set now + 1year expiration for my http responses properly in django?
Can I do that using the render()
shortcut?
Upvotes: 1
Views: 3437
Reputation: 1757
Another alternative is to use patch_response_headers(response, cache_timeout=None)
from django.utils.cache
. This will simply set the Expires
and Cache-Control
HTTP headers on the response
object according to the specified timeout in seconds.
For example in your view code you can apply it to your HttpResponse
object before you return it:
from django.utils.cache import patch_response_headers
# Add Expires and Cache-Control headers to cache in browser for 5 minutes
patch_response_headers(response, cache_timeout=300)
return response
This may be a better solution if you only want the browser to cache the response for a specific amount of time with no caching occurring on the server.
See the Django documentation for more details: https://docs.djangoproject.com/en/dev/ref/utils/#django.utils.cache.patch_response_headers
Upvotes: 2
Reputation: 15549
You can do it using decorators, see the relevant docs on using per-view cache.
Upvotes: 2