这对于项目中的1个应用程序(1页)是非常重要的,其他页面将使用一些标准分析产品,如谷歌分析.
我看了django跟踪,但似乎这是过度的,因为我只想在1视图中使用它.理想的情况是,将整个请求对象传递给TaskQue,然后进行处理.所以用户首先被重定向,分析处理将在幕后完成.
解决方法 我们使用一些简单的中间件..下面是摘录.您可以修改它直接在视图中使用.class WebRequest(models.Model): time = models.DateTimeFIEld(auto_Now_add=True) host = models.CharFIEld(max_length=1000) path = models.CharFIEld(max_length=1000) method = models.CharFIEld(max_length=50) uri = models.CharFIEld(max_length=2000) status_code = models.IntegerFIEld() user_agent = models.CharFIEld(max_length=1000,blank=True,null=True) remote_addr = models.IPAddressFIEld() remote_addr_fwd = models.IPAddressFIEld(blank=True,null=True) Meta = models.TextFIEld() cookies = models.TextFIEld(blank=True,null=True) get = models.TextFIEld(blank=True,null=True) post = models.TextFIEld(blank=True,null=True) raw_post = models.TextFIEld(blank=True,null=True) is_secure = models.BooleanFIEld() is_AJAX = models.BooleanFIEld() user = models.ForeignKey(User,null=True)def dumps(value): return Json.dumps(value,default=lambda o:None)class WebRequestMIDdleware(object): def process_vIEw(self,request,vIEw_func,vIEw_args,vIEw_kwargs): setattr(request,'hIDe_post',vIEw_kwargs.pop('hIDe_post',False)) def process_response(self,response): if request.path.endswith('/favicon.ico'): return response if type(response) == httpResponsePermanentRedirect and settings.APPEND_SLASH: new_location = response.get('location',None) content_length = response.get('content-length',None) if new_location and content_length is '0': new_parsed = urlparse(new_location) old = (('http','https')[request.is_secure()],request.get_host(),'{0}/'.format(request.path),request.Meta['query_STRING']) new = (new_parsed.scheme,new_parsed.netloc,new_parsed.path,new_parsed.query) if old == new: #dont log - it's just adding a / return response try: self.save(request,response) except Exception as e: print >> sys.stderr,"Error saving request log",e return response def save(self,response): if hasattr(request,'user'): user = request.user if type(request.user) == User else None else: user = None Meta = request.Meta.copy() Meta.pop('query_STRING',None) Meta.pop('http_cookie',None) remote_addr_fwd = None if 'http_X_FORWARDED_FOR' in Meta: remote_addr_fwd = Meta['http_X_FORWARDED_FOR'].split(",")[0].strip() if remote_addr_fwd == Meta['http_X_FORWARDED_FOR']: Meta.pop('http_X_FORWARDED_FOR') post = None uri = request.build_absolute_uri() if request.POST and uri != '/login/': post = dumps(request.POST) models.WebRequest( host = request.get_host(),path = request.path,method = request.method,uri = request.build_absolute_uri(),status_code = response.status_code,user_agent = Meta.pop('http_USER_AGENT',None),remote_addr = Meta.pop('REMOTE_ADDR',remote_addr_fwd = remote_addr_fwd,Meta = None if not Meta else dumps(Meta),cookies = None if not request.cookieS else dumps(request.cookieS),get = None if not request.GET else dumps(request.GET),post = None if (not request.POST or getattr(request,'hIDe_post') == True) else dumps(request.POST),raw_post = None if getattr(request,'hIDe_post') else request.raw_post_data,is_secure = request.is_secure(),is_AJAX = request.is_AJAX(),user = user ).save()总结
以上是内存溢出为你收集整理的Django保存整个统计数据请求,有什么可用?全部内容,希望文章能够帮你解决Django保存整个统计数据请求,有什么可用?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)