From c46bb219bfd5cab84e20fc9e87238093d36ecf52 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Mon, 25 Aug 2008 04:52:55 +0000 Subject: [PATCH] Fixed #7460 -- Made the "cache" template tag always generate keys that can be used with the memcache backend (which has the strongest restriction on keys). Based on a patch from trbs. git-svn-id: http://code.djangoproject.com/svn/django/trunk@8533 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/templatetags/cache.py | 3 ++- tests/regressiontests/templates/tests.py | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/django/templatetags/cache.py b/django/templatetags/cache.py index 07bd590fba..9c6ca76854 100644 --- a/django/templatetags/cache.py +++ b/django/templatetags/cache.py @@ -2,6 +2,7 @@ from django.template import Library, Node, TemplateSyntaxError, Variable, Variab from django.template import resolve_variable from django.core.cache import cache from django.utils.encoding import force_unicode +from django.utils.http import urlquote register = Library() @@ -22,7 +23,7 @@ class CacheNode(Node): except (ValueError, TypeError): raise TemplateSyntaxError('"cache" tag got a non-integer timeout value: %r' % expire_time) # Build a unicode key for this fragment and all vary-on's. - cache_key = u':'.join([self.fragment_name] + [force_unicode(resolve_variable(var, context)) for var in self.vary_on]) + cache_key = u':'.join([self.fragment_name] + [urlquote(resolve_variable(var, context)) for var in self.vary_on]) value = cache.get(cache_key) if value is None: value = self.nodelist.render(context) diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index 96e1d662c6..0eb5a2a586 100644 --- a/tests/regressiontests/templates/tests.py +++ b/tests/regressiontests/templates/tests.py @@ -917,6 +917,9 @@ class Templates(unittest.TestCase): 'cache14': ('{% load cache %}{% cache foo bar %}{% endcache %}', {'foo': 'fail'}, template.TemplateSyntaxError), 'cache15': ('{% load cache %}{% cache foo bar %}{% endcache %}', {'foo': []}, template.TemplateSyntaxError), + # Regression test for #7460. + 'cache16': ('{% load cache %}{% cache 1 foo bar %}{% endcache %}', {'foo': 'foo', 'bar': 'with spaces'}, ''), + ### AUTOESCAPE TAG ############################################## 'autoescape-tag01': ("{% autoescape off %}hello{% endautoescape %}", {}, "hello"), 'autoescape-tag02': ("{% autoescape off %}{{ first }}{% endautoescape %}", {"first": "hello"}, "hello"),