我希望能够跟踪我的内部应用程序的经过身份验证的用户页面访问,我查看https://djangopackages.org/grids/g/analytics/但我看不到符合要求的用户。
我需要知道的是我的authenciated用户正在访问的内容以及访问次数。
例如,或者某些类似的
User | logins | total page visits | most visited url | last visited url
John Smith | 100 | 2000 | sitedetails/1 | sitedetails/50
由于
答案 0 :(得分:1)
我需要知道的是我的authenciated用户正在访问的内容以及访问次数。
首先想到的是创建一个带有用户外键的模型和一个他们请求的视图的字段。
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"
)
如果您花时间将这个逻辑隔离成一个写得很好的函数,并在每个视图的开头调用它。
def another_view(req, *a, **kw):
count_request() # all logic implemented inside this func.
或者使用基于类的视图。
class RequestCounterView(View):
def dispatch(req, *a, **kw):
# do request counting
return super(RequestCounterView, self).dispatch(*args, **kwargs)
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.