且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何访问模板缓存? - Django

更新时间:2023-01-09 08:06:20

这是我在项目中访问模板缓存的方式:

This is how I access the template cache in my project:

from django.utils.hashcompat import md5_constructor
from django.utils.http import urlquote

def someView(request):
    variables = [var1, var2, var3] 
    hash = md5_constructor(u':'.join([urlquote(var) for var in variables]))
    cache_key = 'template.cache.%s.%s' % ('table', hash.hexdigest())

    if cache.has_key(cache_key):
        #do some stuff...

我使用缓存标签的方式,我有:

The way I use the cache tag, I have:

    {% cache TIMEOUT table var1 var2 var3 %}

您可能只需将空列表传递给变量
所以,您的变量和缓存_ 将如下所示:

You probably just need to pass an empty list to variables. So, yourvariables and cache_key will look like:

    variables = []
    hash = md5_constructor(u':'.join([urlquote(var) for var in variables]))
    cache_key = 'template.cache.%s.%s' % ('stats', hash.hexdigest())