这是因为实际的键不是“类别”,而是由 Django 使用以下内容动态构建的:
args = md5_constructor(u':'.join([urlquote(resolve_variable(var, context)) for var in self.vary_on]))
cache_key = 'template.cache.%s.%s' % (self.fragment_name, args.hexdigest())
见:https ://code.djangoproject.com/browser/django/tags/releases/1.3.1/django/templatetags/cache.py
通常,密钥将采用以下格式:template.cache.categories.[hexdigest]
. 所以棘手的部分是找出 hexdigest 部分。
我发现了以下Django 片段(在评论中),看起来它应该仍然有效(从 2009 年开始):
from django.core.cache import cache
from django.utils.hashcompat import md5_constructor
from django.utils.http import urlquote
def invalidate_template_cache(fragment_name, *variables):
args = md5_constructor(u':'.join([urlquote(var) for var in variables]))
cache_key = 'template.cache.%s.%s' % (fragment_name, args.hexdigest())
cache.delete(cache_key)
由于您没有将任何变量传递给模板标签,因此您可以使用以下方式调用它:invalidate_template_cache('categories')
。否则,您需要传入模板标记所依赖的所有变量的列表作为第二个参数。