2009-05-20 45 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 ...

由于命中表可能正在快速增长,因此您应该考虑删除策略。

代码未经测试

相关问题