Django保存整个请求进行统计,有什么可用的?
我想保存可用于统计的所有内容,例如引荐来源网址、操作系统、浏览器等。有哪些可用的以及存储它们的最佳方式是什么?
这仅对项目中的 1 个应用程序(1 个页面)很重要,其他页面将使用一些标准分析产品,例如 google 分析。
我查看了 django-tracking,但这似乎有点过分了,因为我只想在 1 个视图上使用它。理想的情况是,将整个请求对象传递给 TaskQue 并稍后进行处理。因此,用户首先被重定向,分析处理将在幕后完成。
I want to save everything that can be used for statistics, such as referrer, os, browser etc. What is available and what's the best way to store it?
This is only important for 1 application (1 page) in the project, the other pages some standard analytics product will be used such as google analytics.
I had a look at django-tracking, but it seems this is overkill as I only want to use it on 1 view. The ideal situation would be, passing the whole request object to a TaskQue and do the processing later. So the user is redirected first and the analytics processing will be done behind the scenes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们使用一些简单的中间件..下面是摘录。您可以修改它以直接在视图中使用。
We use some simple middleware.. below is an excerpt. You can modify it to use directly within a view.
只需手动从请求中提取它即可。
该文档概述了可以从请求对象中提取的大量信息。
例如,标头存储在
request.META
中,GET 参数存储在 request.GET 中,等等。http://docs.djangoproject.com/ en/dev/ref/request-response/#django.http.HttpRequest.META
存储它的最佳方式是什么?取决于你在做什么。记录它,将其存储在数据库中,将其发送到其他地方......你说的是统计数据,所以数据库听起来是一个放置它的好地方,因为它很容易查询。
Just manually pull it from the request.
The docs outlines a lot of the info that can be pulled from the request object.
For example, headers are stored in
request.META
, GET params in request.GET, etc.http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.META
What's the best way to store it? Depends what you're doing. Log it, store it in a DB, send it somewhere else... You say for statistics, so a database sounds like a good place to put this as it's easy to query.
扩展 Josh 的答案,如果您使用 postgres 作为后端,则可以使用 JSONField 来发布数据。它将有助于直接处理 json 而不是手动加载它。
阅读更多: https://docs.djangoproject.com/ en/2.0/ref/contrib/postgres/fields/#jsonfield
你可以这样做
Extension to Josh answer, you could use JSONField for post data if you are using postgres as your backend. It will help in dealing with json directly rather than loading it manually.
read more: https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/fields/#jsonfield
you could do something like this