Skip to content Skip to sidebar Skip to footer

Counting "page Views" Or "hits" When Using Cache

I have a view called show_board. Inside it, among other things, I increment a field Board.views by 1 every time it is run, to count page views. The problem is that when I use the @

Solution 1:

You can actually write another decorator to achieve this.

defview_incrementer(view_func):
    """A decorator which increments board views"""def_wrapped(*args, **kwargs):
        # here you can increment Board.views# kwargs will contain URL parameters
        pk = kwargs.get('pk')
        Board.objects.filter(pk=pk).update(views=F('views')+1)
        return view_func(*args, **kwargs)

    return _wrapped

Then apply the decorator to your view function/method:

@view_incrememter@cache_page(timeout=60)
def show_board(request, pk):
    ...

Or you can use it directly in your urls.py

url(r'^some-path/(?P<pk>\d+)/$', view_incrementer(cache_page(60)(show_board))),

Post a Comment for "Counting "page Views" Or "hits" When Using Cache"