Reputation: 419
I'm creating a django app for comics and having a code in my views.py of an app like this:
@comic_inc
@cache_page(settings.CACHE_S) //CACHE_S = 60
@vary_on_cookie
def comic(request, com):
try:
cobj = Comic.gen_comic(com, request.user.id)
if request.GET.get('sort') == '2':
chaps = Chapter.objects.filter(published=True, comic = cobj).order_by('num','snum','volume')
else:
chaps = Chapter.objects.filter(published=True, comic = cobj).order_by('-num','-snum','-volume')
return render(request, 'base/comic.html', {'comic': cobj, 'chapters':chaps})
except Exception as e:
return render(request, 'base/comic.html', {'error': 'Comic not found'})
the @comic_inc
is a decorator I created when I was trying to implement this solution: Counting "page views" or "hits" when using cache
the decorator code is as below:
def comic_inc(view_func):
def _wrapped(*args,**kwargs):
Comic.objects.filter(slug=kwargs.get('com')).update(pageviews=F('pageviews')+1)
return view_func(*args,**kwargs)
return _wrapped
I didn't particularly specify any CACHE
in django settings since there is already a default cache django.core.cache.backends.locmem.LocMemCache that works perfectly fine for development (shows up properly in debug toolbar). Additionally I did try memcached (downloaded and ran memcached -m 512 -vvv
in CMD and it worked perfectly).
Either way the result was the same, the pageviews is updated only when cached page times out and only increased by 1. I don't mind that the view value is not changed for every refresh on the cached template page but I atleast want the value to be increased on the backend. I did verify if the value of pageviews is changing by refereshing about 20 times on the template and then checking the admin panel and also waiting for timeout and refreshing again, but the value didn't increase more than 1.
Please help me.
Upvotes: 0
Views: 200
Reputation: 50806
Remove any caching-related middleware from your settings.py
if you want to use the cache_page
decorator like that. The middleware would be responsible for easy site-wide caching while the decorator is useful for more configurable page/view-specific caching.
Upvotes: 1