djanghitCount显示每个记录的“hit”的日期和ip

2024-05-13 16:58:58 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个无足轻重的问题,请原谅我的不理解。在

我使用的是python3.6&django 1.10.5。在

我得到了一个测试页面,它现在使用django-hitcount来跟踪文档被“点击”的次数—查看和下载。这看起来还不错。在

现在我想扩展代码,这样我就可以在每次文档“命中”时显示日期和ip。在

我查看了db表,日期和ip存储在django hitcount表中,如下所示:

enter image description here

这是视图.py显示记录的命中率的代码:

from hitcount.models import HitCount
....
document_hits = HitCount.objects.filter(
    content_type=content_type, modified__gte=one_week_ago).order_by('object_pk')

我已经阅读了django-hitcount docs,但是我无法理解如何编写/扩展视图以显示每次命中的日期和ip。在

我试过很多方法,但我缺少一些基本的东西。在

如果能帮助我朝着正确的方向前进,我将不胜感激。在


Tags: django代码文档pyip视图dbtype
1条回答
网友
1楼 · 发布于 2024-05-13 16:58:58

通过研究django-hitcount documentation on displayingdjango-hitcount github models.py,我们发现有一个class Hit有一个ForeignKey到{},默认的反向关系是名称hits,因此我们可以推断出,显示诸如每次命中的日期和IP等信息的方法是在项目中添加以下内容:

A视图:

from hitcount.views import HitCountDetailView

class YourModelCountHitDetailView(HitCountDetailView):
    model = YourModel        # your model goes here
    count_hit = True    # set to True if you want it to try and count the hit

以及模板:

^{pr2}$

相关问题 更多 >