2009-05-20 54 views

回答

1

查看request.path的中間件很難看,因爲它引入了對用來顯示文章和博客文章的URL模式細節的依賴。如果你不介意這種耦合,那麼你可能只需保存性能點擊並在Web服務器日誌文件上進行分析即可。 (編輯view middleware將是一個更好的選擇,因爲它給你視圖可調用和它的參數。我仍然更喜歡裝飾器的方法,因爲它不會導致不相關的視圖的開銷,但查看中間件將工作,如果你不想要觸摸博客/文章應用程序的URLconf)。

我會使用一個視圖裝飾器,你包裹object_detail視圖(或您的自定義等效)。你可以直接在URLconf中進行封裝。事情是這樣的:

def count_hits(func): 
    def decorated(request, *args, **kwargs): 
     # ... find object and update hit count for it... 
     return func(request, *args, **kwargs) 
    return decorated 

而且你可以在views.py應用它:

@count_hits 
def detail_view(... 

或在URLconf:

url(r'^/blog/post...', count_hits(detail_view)) 
0

,你可以創建一個通用的打擊模型

class Hit(models.Model): 
    date = models.DateTimeFiles(auto_now=True) 
    content_type = models.ForeignKey(ContentType) 
    object_id = models.PositiveIntegerField() 
    content_object = generic.GenericForeignKey('content_type', 'object_id') 

在你的view.py中你寫這個函數:

def render_to_response_hit_count(request,template_path,keys,response): 
    for key in keys: 
     for i in response[key]: 
      Hit(content_object=i).save() 
    return render_to_response(template_path, response) 

和您感興趣的回報

return render_to_response_hit_count(request, 'map/list.html',['list',], 
     { 
      'list': l, 
     }) 

這種方法給你的權力,不僅要算命中,但時間篩選命中的歷史,則contentType等的意見on ...

由於命中表可能正在快速增長,因此您應該考慮刪除策略。

代碼未經測試

相關問題