Browse files

magic-removal: Refactored never_cache decorator to provide a utility

function that can be used inside views, and added docs.

git-svn-id: bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
spookylukey committed Mar 30, 2006
1 parent 8102d12 commit 4d76b4df25d0270d3b9885a2c8fa2efcbf58a024
Showing with 21 additions and 3 deletions.
  1. +7 −0 django/utils/
  2. +2 −3 django/views/decorators/
  3. +12 −0 docs/cache.txt
@@ -84,6 +84,13 @@ def patch_response_headers(response, cache_timeout=None):
cache_timeout = 0 # Can't have max-age negative
patch_cache_control(response, max_age=cache_timeout)
+def add_never_cache_headers(response):
+ """
+ Add headers to a response to indicate that
+ a page should never be cached.
+ """
+ patch_response_headers(response, cache_timeout=-1)
def patch_vary_headers(response, newheaders):
Adds (or updates) the "Vary" header in the given HttpResponse object.
@@ -13,7 +13,7 @@
import re
from django.utils.decorators import decorator_from_middleware
-from django.utils.cache import patch_cache_control, patch_response_headers
+from django.utils.cache import patch_cache_control, add_never_cache_headers
from django.middleware.cache import CacheMiddleware
cache_page = decorator_from_middleware(CacheMiddleware)
@@ -31,14 +31,13 @@ def _cache_controlled(request, *args, **kw):
return _cache_controller
def never_cache(view_func):
Decorator that adds headers to a response so that it will
never be cached.
def _wrapped_view_func(request, *args, **kwargs):
response = view_func(request, *args, **kwargs)
- patch_response_headers(response, cache_timeout=-1)
+ add_never_cache_headers(response)
return response
return _wrapped_view_func
@@ -332,6 +332,18 @@ the value of the ``CACHE_MIDDLEWARE_SETTINGS`` setting. If you use a custom
``max_age`` in a ``cache_control`` decorator, the decorator will take
precedence, and the header values will be merged correctly.)
+If you want to use headers to disable caching altogether, two utility functions
+are provided. `django.utils.cache.add_never_cache_headers`` is a
+function that takes a single HttpResponse object as its argument and adds
+headers to ensure the response won't be cached by browsers or other caches.
+``django.views.decorators.never_cache`` is a view decorator that does the same
+thing but can be applied to a view function for convenience. Example::
+ from django.views.decorators.cache import never_cache
+ @never_cache
+ def myview(request):
+ ...
.. _`Cache-Control spec`:
Other optimizations

0 comments on commit 4d76b4d

Please sign in to comment.