Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Looked up the template_fragments cache at runtime.

  • Loading branch information...
commit 3380495e93f5e81b80a251b03ddb0a80b17685f5 1 parent 905a74f
@aaugustin aaugustin authored
Showing with 10 additions and 11 deletions.
  1. +9 −10 django/templatetags/cache.py
  2. +1 −1  tests/template_tests/tests.py
View
19 django/templatetags/cache.py
@@ -1,16 +1,11 @@
from __future__ import unicode_literals
-from django.core.cache import caches, InvalidCacheBackendError
+from django.core.cache import cache, caches, InvalidCacheBackendError
from django.core.cache.utils import make_template_fragment_key
from django.template import Library, Node, TemplateSyntaxError, VariableDoesNotExist
register = Library()
-try:
- default_cache = caches['template_fragments']
-except InvalidCacheBackendError:
- from django.core.cache import cache as default_cache
-
class CacheNode(Node):
def __init__(self, nodelist, expire_time_var, fragment_name, vary_on, cache_name):
@@ -35,17 +30,21 @@ def render(self, context):
except VariableDoesNotExist:
raise TemplateSyntaxError('"cache" tag got an unknown variable: %r' % self.cache_name.var)
try:
- cache = caches[cache_name]
+ fragment_cache = caches[cache_name]
except InvalidCacheBackendError:
raise TemplateSyntaxError('Invalid cache name specified for cache tag: %r' % cache_name)
else:
- cache = default_cache
+ try:
+ fragment_cache = caches['template_fragments']
+ except InvalidCacheBackendError:
+ fragment_cache = caches['default']
+
vary_on = [var.resolve(context) for var in self.vary_on]
cache_key = make_template_fragment_key(self.fragment_name, vary_on)
- value = cache.get(cache_key)
+ value = fragment_cache.get(cache_key)
if value is None:
value = self.nodelist.render(context)
- cache.set(cache_key, value, expire_time)
+ fragment_cache.set(cache_key, value, expire_time)
return value
View
2  tests/template_tests/tests.py
@@ -514,7 +514,7 @@ def test_cache_fragment_cache(self):
o2 = t2.render(ctx)
self.assertEqual(o1, 'foo')
- self.assertNotEqual(o1, o2)
+ self.assertEqual(o2, 'bar')
def test_cache_missing_backend(self):
"""
Please sign in to comment.
Something went wrong with that request. Please try again.