我希望能够跟踪经过身份验证的用户对我的内部应用程序的页面访问,我查看了 https://djangopackages.org/grids/g/analytics/但我看不到符合要求的。
我只需要知道经过身份验证的用户正在访问什么以及访问了多少次。
例如,或者类似的东西
User | logins | total page visits | most visited url | last visited url
John Smith | 100 | 2000 | sitedetails/1 | sitedetails/50
谢谢
最佳答案
All i need to know is what my authenciated users are visiting and how many times.
首先想到的是创建一个模型,其中包含用户的外键和他们请求的 View 的字符字段。
class Request(models.Model):
user = models.ForeignKey(User)
view = models.CharField(max_length=250) # this could also represent a URL
visits = models.PositiveIntegerField()
这将使您能够计算用户点击页面的次数。
def some_view(req, *a, **kw):
# try to find the current users request counter object
request_counter = Request.objects.filter(
user__username=req.user.username,
view="some_view"
)
if request_counter:
# if it exists add to it
request_counter[0].visits += 1
request_counter.save()
else:
# otherwise create it and set its visits to one.
Request.objects.create(
user=req.user,
visits=1,
view="some_view"
)
如果您花时间,可以将此逻辑隔离到一个编写良好的函数中,并在每个 View 的开头调用它。
def another_view(req, *a, **kw):
count_request() # all logic implemented inside this func.
或者使用基于类的 View 。
class RequestCounterView(View):
def dispatch(req, *a, **kw):
# do request counting
return super(RequestCounterView, self).dispatch(*a, **kw)
class ChildView(RequestCounterView):
def get(req, *a, **kw):
# continue with regular view
# this and all other views that inherit
# from RequestCounterView will inherently
# count their requests based on user.
关于Django - 跟踪经过身份验证的用户页面访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38658751/