1
2
3
4
5
6
7
8
9
10
11
12
13
14 | from django.middleware.cache import CacheMiddleware
class VaryOnLangCacheMiddleware(CacheMiddleware):
def __init__(self, **kwargs):
CacheMiddleware.__init__(self, **kwargs)
self.ori_key_prefix = self.key_prefix
def process_request(self, request):
# Reset key_prefix depending on language
lang_suffix = '_%s' % request.LANGUAGE_CODE
if not self.key_prefix.endswith(lang_suffix):
self.key_prefix = self.ori_key_prefix + lang_suffix
return CacheMiddleware.process_request(self, request)
|
Comments
This snippet should not be necessary.
If your view's output depends on the language, just make sure that your HttpResponse has a Vary header that indicates Accept-Language.
(This is still an issue if you use the low-level cache, but not the middleware or decorators.)
Note that if you're using the LocaleMiddleware, you should put this before the CacheMiddleware in your MIDDLEWARE_CLASSES. Order of middleware is important.
If this snippet is, in fact, needed for some reason, it's likely a bug in Django. Rather than using this snippet, please report the details of the bug.
#