diff --git a/django/utils/cache.py b/django/utils/cache.py index b1b35b5a05..04beccaef2 100644 --- a/django/utils/cache.py +++ b/django/utils/cache.py @@ -101,7 +101,7 @@ def patch_response_headers(response, cache_timeout=None): cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS if cache_timeout < 0: cache_timeout = 0 # Can't have max-age negative - if not response.has_header('ETag'): + if settings.USE_ETAGS and not response.has_header('ETag'): response['ETag'] = '"%s"' % md5_constructor(response.content).hexdigest() if not response.has_header('Last-Modified'): response['Last-Modified'] = http_date() diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt index 47fa486fe0..3a7d987d44 100644 --- a/docs/ref/settings.txt +++ b/docs/ref/settings.txt @@ -1825,8 +1825,9 @@ USE_ETAGS Default: ``False`` A boolean that specifies whether to output the "Etag" header. This saves -bandwidth but slows down performance. This is only used if ``CommonMiddleware`` -is installed (see :doc:`/topics/http/middleware`). +bandwidth but slows down performance. This is used by the ``CommonMiddleware`` +(see :doc:`/topics/http/middleware`) and in the``Cache Framework`` +(see :doc:`/topics/cache`). .. setting:: USE_I18N diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py index 82ab47c6c4..941ab70b8a 100644 --- a/tests/regressiontests/cache/tests.py +++ b/tests/regressiontests/cache/tests.py @@ -1048,6 +1048,7 @@ class CacheI18nTest(unittest.TestCase): settings.CACHE_MIDDLEWARE_SECONDS = 60 settings.CACHE_MIDDLEWARE_KEY_PREFIX="test" settings.CACHE_BACKEND='locmem:///' + settings.USE_ETAGS = True settings.USE_I18N = True en_message ="Hello world!" es_message ="Hola mundo!" @@ -1058,6 +1059,14 @@ class CacheI18nTest(unittest.TestCase): # Check that we can recover the cache self.assertNotEqual(get_cache_data.content, None) self.assertEqual(en_message, get_cache_data.content) + # Check that we use etags + self.assertTrue(get_cache_data.has_header('ETag')) + # Check that we can disable etags + settings.USE_ETAGS = False + request._cache_update_cache = True + set_cache(request, 'en', en_message) + get_cache_data = FetchFromCacheMiddleware().process_request(request) + self.assertFalse(get_cache_data.has_header('ETag')) # change the session language and set content request = self._get_request_cache() set_cache(request, 'es', es_message)