Django访问量和页面点击数统计
2021/5/12 18:55:16
本文主要是介绍Django访问量和页面点击数统计,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
看了很多文章主要的实现方法就三种:
1.简单的模板页面计数的实现
下面是在模板中做一个简单的页面点击数统计、model阅读量统计、用户访问量统计的方法
2.model对象的计数器实现
Django hit counter application that tracks the number of hits/views for chosen objects.
hit counter是用来计数model对象的访问次数的。
安装django-hitcount:
3.页面的用户访问量统计
django-tracking
keeps track of visitors to Django-powered Web sites. It also offers basic blacklisting capabilities.
安装django-tracking
这是着重说一下第三种实现:
跟着官方的配置往下装就好,
1.安装django-tracking
pip install django-tracking
一上来就出错也是真的无语,一开始以为是pip的问题,仔细一看是Django的错django.core.exceptions.ImproperlyConfigured:
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. ---------------------------------------- Command "python setup.py egg_info" failed with error code 1 in c:\users\admini~1\appdata\local\temp\pip-build-zgcy_f\django-tracking\在命令行执行先
set DJANGO_SETTINGS_MODULE = mysite . settings
再执行pip install django-tracking
django.core.exceptions.ImproperlyConfigured:
2.在setting.py
INSTALLED_APPS
中添加App
First of all, you must add this project to your list of
INSTALLED_APPS
in
settings.py
:
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', ... 'tracking', ... )
3.新建表
Run manage.py syncdb. This creates a few tables in your database that arenecessary for operation.
在这里也遇了点小麻烦
提示 HAS_GEOIP找不到 但我往上看了下代码,是有的只是个bool值
上一级打印为FALSE 报错的地方之间屏蔽了,再次躲过一劫
4.添加中间件
我只用到了第一个。。。
Visitor Tracking
Add tracking.middleware.VisitorTrackingMiddleware
to yourMIDDLEWARE_CLASSES
insettings.py
. It must be underneath theAuthenticationMiddleware
, so thatrequest.user
exists.
Automatic Visitor Clean-Up
If you want to have Django automatically clean past visitor information outyour database, puttracking.middleware.VisitorCleanUpMiddleware
in yourMIDDLEWARE_CLASSES
.
IP Banning
Add tracking.middleware.BannedIPMiddleware
to your MIDDLEWARE_CLASSES
insettings.py
. I would recommend making this the very first item inMIDDLEWARE_CLASSES
so your banned users do not have to drill through anyother middleware before Django realizes they don't belong on your site.
Visitors on Page (template tag)
Make sure that django.core.context_processors.request
is somewhere in yourTEMPLATE_CONTEXT_PROCESSORS
tuple. This context processor makes therequest
object accessible to your templates. This application uses therequest
object to determine what page the user is looking at in a templatetag.
settings.py 还有一些配置:
-
GOOGLE_MAPS_KEY
: Your very own Google Maps API key -
TRACKING_USE_GEOIP
: set this toTrue
if you want to see markers onthe map -
GEOIP_PATH
: set this to the absolute path on the filesystem of yourGeoIP.dat
orGeoIPCity.dat
or whatever file. It's usually somethinglike/usr/local/share/GeoIP.dat
or/usr/share/GeoIP/GeoIP.dat
. -
GEOIP_CACHE_TYPE
: The type of caching to use when dealing with GeoIP data:-
0
: read database from filesystem, uses least memory. -
1
: load database into memory, faster performance but uses morememory. -
2
: check for updated database. If database has been updated, reloadfilehandle and/or memory cache. -
4
: just cache the most frequently accessed index portion of thedatabase, resulting in faster lookups thanGEOIP_STANDARD
, but lessmemory usage thanGEOIP_MEMORY_CACHE
- useful for larger databasessuch as GeoIP Organization and GeoIP City. Note, for GeoIP Country,Region and Netspeed databases,GEOIP_INDEX_CACHE
is equivalent toGEOIP_MEMORY_CACHE
.default。
-
5.添加模版
Usage
To display the number of active users there are in one of your templates, makesure you have{% load tracking_tags %}
somewhere in your template and dosomething like this:
{% load tracking_tags %}
{% visitors_on_site as visitors %} <p> {{ visitors }} active user{{ visitors|pluralize }} </p>
If you also want to show how many people are looking at the same page:
{% visitors_on_page as same_page %} <p> {{ same_page }} of {{ visitors }} active user{{ visitors|pluralize }} {{ same_page|pluralize:"is,are" }} reading this page </p>
然后重启下服务还遇到了两个问题
应该是Django-tracking只支持到Django1.6吧
在新版本中这些模块都做改动了
原因在于:Django 1.6 时改变了模块结构,原先的defaults模块被去除了。
找到代码问题行,将此行改为
# from django.conf.urls.defaults import * from django.conf.urls import patterns, url, include
问题
C:\Python27\lib\site-packages\tracking\views.py in <module>, line 9
解决
原因在于:simplejson模块被去除了
找到代码问题行,将此行改为
from json import JSONEncoder
问题
AttributeError 'VisitorManager' object has no attribute 'get_query_set'
解决
原因在于:父类Manager中 get_query_set 更名为 get_queryset
return self.get_queryset().filter(last_update__gte=cutoff)
可以看下GitHub https://github.com/bruth/django-tracking2
django-tracking2 对 django-tracking的优化
django-tracking2 tracks the length of time visitors and registered users spend on your site. Although this will work for websites, this is more applicable to web applications with registered users. This does not replace (nor intend) to replace client-side analytics which is great for understanding aggregate flow of page views.
Note: This is not a new version of django-tracking. These apps have very different approaches and, ultimately, goals of tracking users. This app is about keeping a history of visitor sessions, rather than the current state of the visitor.
这篇关于Django访问量和页面点击数统计的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23MongoDB身份认证机制揭秘!
- 2024-11-20MongoDB教程:从入门到实践详解
- 2024-11-17执行 Google Ads API 查询后返回的是空数组什么原因?-icode9专业技术文章分享
- 2024-11-17google广告数据不同经理账户下的凭证可以获取对方的api数据吗?-icode9专业技术文章分享
- 2024-11-15SendGrid 的 Go 客户端库怎么实现同时向多个邮箱发送邮件?-icode9专业技术文章分享
- 2024-11-15SendGrid 的 Go 客户端库怎么设置header 和 标签tag 呢?-icode9专业技术文章分享
- 2024-11-12Cargo deny安装指路
- 2024-11-02MongoDB项目实战:从入门到初级应用
- 2024-11-01随时随地一键转录,Google Cloud 新模型 Chirp 2 让语音识别更上一层楼
- 2024-10-25Google Cloud动手实验详解:如何在Cloud Run上开发无服务器应用