Django访问量和页面点击数统计
来源:互联网 发布:windows10安装ubuntu 编辑:程序博客网 时间:2024/06/13 10:00
看了很多文章主要的实现方法就三种:
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:
INSTALLED_APPS
中添加AppINSTALLED_APPS
insettings.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, put
tracking.middleware.VisitorCleanUpMiddleware
in yourMIDDLEWARE_CLASSES
.IP Banning
Add
tracking.middleware.BannedIPMiddleware
to yourMIDDLEWARE_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 keyTRACKING_USE_GEOIP
: set this toTrue
if you want to see markers onthe mapGEOIP_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吧
在新版本中这些模块都做改动了
# 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
from json import JSONEncoder
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访问量和页面点击数统计
- Django访问量和页面点击数统计
- django 访问量统计 页面点击数 app
- 页面访问量和网站访问量的统计
- jsp统计页面访问量和刷访问量的简单使用
- 页面访问量统计实例
- 统计页面访问量(JSP)
- 统计网站页面的访问量
- 通过文本文件统计页面访问量
- 通过文本文件统计页面访问量
- 访问量和在线人数统计
- 静态HTML页面的访问量统计实现
- java实现页面访问量统计的实例
- Java web----Servlet统计页面访问量
- 并发页面访问量统计简单实现
- 利用ServletContext实现页面访问量的统计
- php通过文本文件统计页面访问量
- 在HTML页面中实现点击数统计
- Git常用命令
- tp 用户登录日志记录
- TS流PCR简介
- Xcode导出审核包后、一个包比一个包大的问题记录
- 5个常见的RuntimeException
- Django访问量和页面点击数统计
- 微信内置浏览器中如何实现点击电话号码自动到拨号页面?
- Day 1 计算机基础及硬件
- 移动端图片上传解决方案localResizeIMG先压缩后ajax无刷新上传
- FPGA学习(第4节)-FPGA设计中的规则技巧要点及常见语法(重要)
- Error和Exception有什么区别
- list to String[]
- Google I/O大会相关
- Java将List,Set,Arrary,String,Object转换为JSON格式