Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed #7460 -- Made the "cache" template tag always generate keys tha…
…t 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
  • Loading branch information
malcolmt committed Aug 25, 2008
1 parent 29d2160 commit c46bb21
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 2 additions & 1 deletion django/templatetags/cache.py
Expand Up @@ -2,6 +2,7 @@
from django.template import resolve_variable from django.template import resolve_variable
from django.core.cache import cache from django.core.cache import cache
from django.utils.encoding import force_unicode from django.utils.encoding import force_unicode
from django.utils.http import urlquote


register = Library() register = Library()


Expand All @@ -22,7 +23,7 @@ def render(self, context):
except (ValueError, TypeError): except (ValueError, TypeError):
raise TemplateSyntaxError('"cache" tag got a non-integer timeout value: %r' % expire_time) 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. # 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) value = cache.get(cache_key)
if value is None: if value is None:
value = self.nodelist.render(context) value = self.nodelist.render(context)
Expand Down
3 changes: 3 additions & 0 deletions tests/regressiontests/templates/tests.py
Expand Up @@ -917,6 +917,9 @@ def get_template_tests(self):
'cache14': ('{% load cache %}{% cache foo bar %}{% endcache %}', {'foo': 'fail'}, template.TemplateSyntaxError), 'cache14': ('{% load cache %}{% cache foo bar %}{% endcache %}', {'foo': 'fail'}, template.TemplateSyntaxError),
'cache15': ('{% load cache %}{% cache foo bar %}{% endcache %}', {'foo': []}, 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 TAG ##############################################
'autoescape-tag01': ("{% autoescape off %}hello{% endautoescape %}", {}, "hello"), 'autoescape-tag01': ("{% autoescape off %}hello{% endautoescape %}", {}, "hello"),
'autoescape-tag02': ("{% autoescape off %}{{ first }}{% endautoescape %}", {"first": "<b>hello</b>"}, "<b>hello</b>"), 'autoescape-tag02': ("{% autoescape off %}{{ first }}{% endautoescape %}", {"first": "<b>hello</b>"}, "<b>hello</b>"),
Expand Down

0 comments on commit c46bb21

Please sign in to comment.