Increment Page Hit Count in Django

前端 未结 4 929
伪装坚强ぢ
伪装坚强ぢ 2020-12-29 08:46

I have a table with an IntegerField (hit_count), and when a page is visited (for example, http://site/page/3) I want record ID 3\'s

4条回答
  •  情话喂你
    2020-12-29 09:10

    As gerdemb says, you should write it into a middleware to make it really reusable. Or (simpler) write a function decorator. In fact, there are adaptors to use a middleware as a decorator and viceversa.

    But if you're worried about performance and want to keep the DB queries per page hit low, you can use memcached's atomic increment operation. of course, in this case, you have to take care of persistence yourself.

提交回复
热议问题