http://blog.csdn.net/pipisorry/article/details/47396311

下面是在模板中做一个简单的页面点击数统计、model阅读量统计、用户访问量统计的方法

简单的模板页面计数的实现

模板中设置:

<li>您是第{{count}}个访问本站的朋友</li>
<li>访问时间:{{time}}</li>

view.py中

def getTime():#获取当前时间
    import time
    return time.ctime()

def getCount():#获取访问次数
    countfile  = open('count.dat','a+')#以读写形式打开记录计数的文件
    counttext = countfile.read()
    try:
        count = int(counttext)+1
    except:
        count = 1
    countfile.seek(0)
    countfile.truncate()#清空文件
    countfile.write(str(count))#重新写入新的访问量
    countfile.flush()
    countfile.close()
    return count

def myHelloWorld(request):
    time = getTime()
    count = getCount()
    para = {"count":count,"time":time}
    ...

这样每次访问时都会调用myHelloWorld函数,读取count值并+1操作

[使用模板做一个站点访问计数器]

[html页面中通过文件对网页访问计数:网页计数器]

http://blog.csdn.net/pipisorry/article/details/47396311

model对象的计数器实现

Django hit counter application that tracks the number of hits/views for chosen objects.

hit counter是用来计数model对象的访问次数的。

安装django-hitcount:

pip install django-hitcount

Settings.py

Add django-hitcount to your INSTALLED_APPS, enableSESSION_SAVE_EVERY_REQUEST:

# settings.py
INSTALLED_APPS = (
    ...
    'hitcount'
)
# needed for django-hitcount to function properly
SESSION_SAVE_EVERY_REQUEST = True

Urls.py

urls.py中加入

# urls.py
urlpatterns = patterns('',
    ...
    url(r'hitcount/', include('hitcount.urls', namespace='hitcount')),
)

View the additional settings section for more information.

Template Magic

Django-hitcount comes packaged with a jQuery implementation that works out-of-the-box to record the
Hits
to an object (be it a blog post, poll, etc). To use thejQuery
implementation
you can either include the app’s script file (as the documentation below shows) or to copy-paste the script into your own jQuery code. Of course: you could also implement this without relying on jQuery.

在需要的模板最开始地方加入loading hitcount tags

{% load hitcount_tags %}

Recording a Hit

If you want to use the jQuery implementation in your project, you can add the Javascript file to your template like so:

{% load staticfiles %}
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="{% static 'hitcount/hitcount-jquery.js' %}"></script>

Then, on your object detail page (blog, page, poll, etc) you inject the needed javascript variables:

# use default insertion method for hitcount-jquery.js:
{% insert_hit_count_js_variables for object %}

# or you can use a template variable to inject as you see fit
{% get_hit_count_js_variables for object as hitcount %}
({ hitcount.ajax_url }}
{{ hitcount.pk }}

Displaying Hit Information

You can retrieve the number of hits for an object many different ways:

# Return total hits for an object:
{% get_hit_count for [object] %}

# Get total hits for an object as a specified variable:
{% get_hit_count for [object] as [var] %}

# Get total hits for an object over a certain time period:
{% get_hit_count for [object] within ["days=1,minutes=30"] %}

# Get total hits for an object over a certain time period as a variable:
{% get_hit_count for [object] within ["days=1,minutes=30"] as [var] %}

[Installation and Usage]

[example project]

[django-hitcount]

http://blog.csdn.net/pipisorry/article/details/47396311

页面的用户访问量统计

django-tracking keeps track of visitors to Django-powered Web sites. It also offers basic blacklisting capabilities.

安装django-tracking

pip install django-tracking

Note:会出错: no module named listeners

配置

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',
    ...
)

Run manage.py syncdb. This creates a few tables in your database that arenecessary for operation.

Depending on how you wish to use this application, you have a few options:

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.

Active Visitors Map

If you're interested in seeing where your visitors are at a given point intime, you might enjoy the active visitor map feature. Be sure you have added aline to your main URLconf, as follows:

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    ....
    (r'^tracking/', include('tracking.urls')),
    ....
)

Next, set a couple of settings in your 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
  • DEFAULT_TRACKING_TEMPLATE: The template to use when generating thevisitor map. Defaults totracking/visitor_map.html.

When that's done, you should be able to go to /tracking/map/ on your site(replacingtracking with whatever prefix you chose to use in your URLconf,obviously). The default template relies upon jQuery for its awesomeness, butyou're
free to use whatever you would like.

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:

{% 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>

If you don't want particular areas of your site to be tracked, you may define alist of prefixes in yoursettings.py using theNO_TRACKING_PREFIXES. Forexample, if you didn't want visits to the/family/ section of your
website,setNO_TRACKING_PREFIXES to['/family/'].

If you don't want to count certain user-agents, such as Yahoo!'s Slurp andGoogle's Googlebot, you may add keywords to your visitor tracking in yourDjango administration interface. Look for "Untracked User-Agents" and add akeyword that distinguishes a particular
user-agent. Any visitors with thekeyword in their user-agent string will not be tracked.

By default, active users include any visitors within the last 10 minutes. Ifyou would like to override that setting, just setTRACKING_TIMEOUT to howevermany minutes you want in yoursettings.py.

For automatic visitor clean-up, any records older than 24 hours are removed bydefault. If you would like to override that setting, setTRACKING_CLEANUP_TIMEOUT to however many hours you want in yoursettings.py.

[django-tracking] from:http://blog.csdn.net/pipisorry/article/details/47396311

ref:在Django中实现一个高性能未读消息计数器

Django访问量和页面点击数统计的更多相关文章

  1. [翻译]在Django项目中添加谷歌统计(Google Analytics)

    原文:<Google Analytics tracking code into Django projects, the easy way> 对我来说,制作一个可扩展的Django应用随时 ...

  2. Django 使用模板页面,块标签,模型

    1.Django 使用模板页面 Django对于成体系的页面提出了模板继承和模板加载的方式. 1.导入静态页面 2.导入静态文件(css,js,images) 3.修改页面当中的静态地址 1.sett ...

  3. Django实现注册页面_头像上传

    Django实现注册页面_头像上传 Django实现注册页面_头像上传 1.urls.py 配置路由 from django.conf.urls import url from django.cont ...

  4. asp.net 访问页面访问统计实现 for iis7

    上一篇博文中< asp.net 访问页面访问统计实现  > 中在win10 (iis8+)上运行没有问题, 但客户机子是windows server 2008  的 iis7弄死不对,最好 ...

  5. asp.net 访问页面访问统计实现

    0x00.背景: 1.用户访问网站所有页面就将访问统计数加1 ,按每月存放. 2.站点并没有用到母版面来实现,所有各个页面都很独立. 3.网站是很早这前的网站,尽量省改动以前的代码.按理说我们应该做一 ...

  6. 利用JS跨域做一个简单的页面访问统计系统

    其实在大部分互联网web产品中,我们通常会用百度统计或者谷歌统计分析系统,通过在程序中引入特定的JS脚本,然后便可以在这些统计系统中看到自己网站页面具体的访问情况.但是有些时候,由于一些特殊情况,我们 ...

  7. grappelli美化django的admin页面

    开始用admin时候,觉得它的页面实在...宁愿自己写modules,多费点时间 grappelli可以把admin变得非常美观,配置起来也很简单 第一步,先下载grappelli,搜索一下,wind ...

  8. [Django] html 前端页面jQuery、图片等路径加载问题

    严格的说这个话题应该属于一个html前端路径加载问题.为了实现一个局部更新页面的功能,简单了解了一下Ajax.Ajax是一个为了实现浏览器和服务器异步通信功能的模块.严格来说不是一个新的语言,只是JS ...

  9. 第二章:2.8 通过Django 在web页面上面输出 “Hello word ”

    1. 第一步:配置 guest 目录下面的 settings.py 文件, 将 sign应用添加到 guest项目中. 2. 在 guest目录下面,打开 urls.py 文件,添加 要打开的路由文件 ...

随机推荐

  1. Activity的四种启动模式任务栈图解

    转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 今天带来另一篇Activity的文章--Activity的四种启动模式.该篇文章,会以图文讲解的方式带你彻底掌握Activity的启动 ...

  2. 计算机网络之局域网&以太网

    局域网的拓扑结构 局域网最主要的特点是:网络为一个单位所拥有,且地理范围和站点数目均有限. 局域网具有广播功能,从一个站点可很方便地访问全网,局域网上的主机可共享连接在局域网上的各种硬件和软件资源. ...

  3. High Executions Of Statement "delete from smon_scn_time..."

    In this Document   Symptoms   Cause   Solution APPLIES TO: Oracle Database - Enterprise Edition - Ve ...

  4. Android自定义View(二、深入解析自定义属性)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51468648 本文出自:[openXu的博客] 目录: 为什么要自定义属性 怎样自定义属性 ...

  5. Apache commons email 使用过程中遇到的问题

    apache-commons-email是对mail的一个封装,所以使用起来确实是很方便.特别的,官网上的tutorial也是极其的简单.但是我也仍然是遇到了没有解决的问题. jar包的添加 mail ...

  6. SQL基本函数

    字符型函数 函数名称 描述 LOWER 将特定的字符串转化为小写,只影响字母字符串. UPPER 将整个字符串转换成大写,只影响字母字符串. INITCAP 将字符串中每一个单词的第一个字母转换为大写 ...

  7. win8如何共享文件夹

    最近小编接手了市委组织部考核项目,各种文档.ER图.原型图,组员之间需要拷来拷去,很不方便,通过飞信,QQ传输吧,文件太大,网络太慢,所以还是不行,于是小编就想起来要共享,以前也映射过别人的共享,觉得 ...

  8. Android-MVVM架构-Data Binding的使用

    项目整体效果: Awesome-Android-MVVM 什么是MVVM, 为什么需要 MVVM? 如何在Android中使用Data Binding实现MVVM架构? 什么是MVVM , 为什么需要 ...

  9. JDBC-数据库的更新操作编程(三)

    首先建立一个静态方法,代码如下: public static Statement getStatement(){ Statement st = null; try { Class.forName(&q ...

  10. 01_Eclipse的使用方法

     1 选择工作站 Workspace:表示工作站 2 切换工作站 选择工作站的方式:File->SwitchWorkspace 这里选择其他的工作站 清除工作站的方式: 找到MyEclips ...