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:



2.在setting.py INSTALLED_APPS 中添加App
First of all, you must add this project to your list of INSTALLED_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, puttracking.middleware.VisitorCleanUpMiddleware in yourMIDDLEWARE_CLASSES.

IP Banning

Add tracking.middleware.BannedIPMiddleware to your MIDDLEWARE_CLASSESinsettings.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 to True 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模块时的错误:no module named django.conf.urls.defaults
解决:

原因在于:Django 1.6 时改变了模块结构,原先的defaults模块被去除了。

找到代码问题行,将此行改为

# from django.conf.urls.defaults import *from django.conf.urls import patterns,  url,  include


问题

ImportErrorNo module named simplejson
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.


原创粉丝点击