简体   繁体   中英

Django @cache_page with SITE_ID (Site Framework)

I'm using Django's Site Framework to run multiple websites and each SITE_ID is based on current city data.

Any way to @cache_page so Django will cache the correct view based on SITE_ID ?

This doesnt work as Django will return the same cached value on all websites regardless of SITE_ID

@cache_page(24 * 60 * 60)
def my_page(request):
   myview = table.objects.filter(city_id=settings.SITE_ID)
   #...

you could do something like: this line talks about this

@cache_page(24*60*60, key_prefix="%s" % settings.SITE_ID) 
def my_page(request):
   myview = table.objects.filter(city_id=settings.SITE_ID)
   #...

or better cache the querysets which is easier to controll

def my_page(request):
   cities = cache.get('cities_%s' % settings.SITE_ID)
   if not cities: 
       cities = table.objects.filter(city_id=settings.SITE_ID)
       cache.set('cities_%s' % settings.SITE_ID, cities, 24*60*60)
   #...
   #...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM