Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

magic-removal: Refactored never_cache decorator to provide a utility

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


git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2602 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 4d76b4df25d0270d3b9885a2c8fa2efcbf58a024 1 parent 8102d12
Luke Plant authored March 30, 2006
7  django/utils/cache.py
@@ -84,6 +84,13 @@ def patch_response_headers(response, cache_timeout=None):
84 84
         cache_timeout = 0 # Can't have max-age negative
85 85
     patch_cache_control(response, max_age=cache_timeout)
86 86
 
  87
+def add_never_cache_headers(response):
  88
+    """
  89
+    Add headers to a response to indicate that 
  90
+    a page should never be cached.
  91
+    """
  92
+    patch_response_headers(response, cache_timeout=-1)
  93
+
87 94
 def patch_vary_headers(response, newheaders):
88 95
     """
89 96
     Adds (or updates) the "Vary" header in the given HttpResponse object.
5  django/views/decorators/cache.py
@@ -13,7 +13,7 @@
13 13
 import re
14 14
 
15 15
 from django.utils.decorators import decorator_from_middleware
16  
-from django.utils.cache import patch_cache_control, patch_response_headers
  16
+from django.utils.cache import patch_cache_control, add_never_cache_headers
17 17
 from django.middleware.cache import CacheMiddleware
18 18
 
19 19
 cache_page = decorator_from_middleware(CacheMiddleware)
@@ -31,7 +31,6 @@ def _cache_controlled(request, *args, **kw):
31 31
 
32 32
     return _cache_controller
33 33
 
34  
-
35 34
 def never_cache(view_func):
36 35
     """
37 36
     Decorator that adds headers to a response so that it will
@@ -39,6 +38,6 @@ def never_cache(view_func):
39 38
     """
40 39
     def _wrapped_view_func(request, *args, **kwargs):
41 40
         response = view_func(request, *args, **kwargs)
42  
-        patch_response_headers(response, cache_timeout=-1)
  41
+        add_never_cache_headers(response)
43 42
         return response
44 43
     return _wrapped_view_func
12  docs/cache.txt
@@ -332,6 +332,18 @@ the value of the ``CACHE_MIDDLEWARE_SETTINGS`` setting. If you use a custom
332 332
 ``max_age`` in a ``cache_control`` decorator, the decorator will take
333 333
 precedence, and the header values will be merged correctly.)
334 334
 
  335
+If you want to use headers to disable caching altogether, two utility functions
  336
+are provided.  `django.utils.cache.add_never_cache_headers`` is a
  337
+function that takes a single HttpResponse object as its argument and adds
  338
+headers to ensure the response won't be cached by browsers or other caches.
  339
+``django.views.decorators.never_cache`` is a view decorator that does the same
  340
+thing but can be applied to a view function for convenience.  Example::
  341
+
  342
+    from django.views.decorators.cache import never_cache
  343
+    @never_cache
  344
+    def myview(request):
  345
+        ...
  346
+
335 347
 .. _`Cache-Control spec`: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
336 348
 
337 349
 Other optimizations

0 notes on commit 4d76b4d

Please sign in to comment.
Something went wrong with that request. Please try again.