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

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

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

模板中设置:

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

view.py中

def getTime():#获取当前时间import timereturn time.ctime()def getCount():#获取访问次数countfile  = open('count.dat','a+')#以读写形式打开记录计数的文件counttext = countfile.read()   try:count = int(counttext)+1except:count = 1    countfile.seek(0)countfile.truncate()#清空文件countfile.write(str(count))#重新写入新的访问量
    countfile.flush()countfile.close()return countdef 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实战: 开发网页计数器统计页面浏览次数

    实际Web开发过程中,我们经常要统计并显示一个页面的浏览次数.今天我们会以博客的例子,教你如何利用Django开发网页计数器,统计并显示一篇文章的浏览次数.本文的原理可以适用于很多场景,比如统计某一文 ...

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

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

  3. python django 动态网页_使用Django创建动态页面

    将 URL 映射到视图 那么概括起来,该视图函数返回了包含当前日期和时间的一段 HTML 页面.但是如何告诉 Django 使用这段代码呢?这就是 URLconfs 粉墨登场的地方了. URLconf ...

  4. 微信小程序页面停留时间统计

    近来在研究微信小程用户是否在使用小程序或者查看用户在小程序停留的时间,无意中在git上找到了相关的解决问题方法,希望正在开发这个功能的的你,能帮助你解决! 收到一个需求,要统计一个用户在我们小程序的每 ...

  5. html页面显示用户在线统计,在HTML页面中实现点击数统计

    在文章发布系统中采用服务器端生成静态页面的方法可以有效减轻服务器的负担,特别是对大流量网站非常有效.但是既然生成的是静态页面,生成时是什么样,显示就是什么样了,对于文章常见文章被阅读次数怎么显示呢? ...

  6. html记录文章页等页面点击数,在HTML页面中实现点击数统计

    在文章发布系统中采用服务器端生成静态页面的方法可以有效减轻服务器的负担,特别是对大流量网站非常有效.但是既然生成的是静态页面,生成时是什么样,显示就是什么样了,对于文章常见文章被阅读次数怎么显示呢? ...

  7. Laravel 页面 PV 统计实例

    目录 统计进行的背景 统计的构思 统计的代码实现 需求背景 在不接入第三方系统的情况下,本系统进行以下数据的统计.每个Mac地址算一个独立访客.IP 不能算独立访客,同一个局域网对外属于一个IP.未找 ...

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

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

  9. 前端页面速度统计方法

    如何统计首屏时间 在页面的各个阶段,将时间打印出来,亦或者是使用html5新增的接口:performance来评估一下自己的网站到底差在哪里(如图). 网页最开始的跳转时间:HTML5的perform ...

  10. Django中重定向页面的时候使用命名空间

    urls.py from django.urls import path from . import viewsapp_name='front'urlpatterns = [path('',views ...

最新文章

  1. 通过IP地址和子网掩码与运算计算相关地址
  2. 无需「域外」文本,微软:NLP就应该针对性预训练
  3. linux 发送邮件
  4. 全国大学生智能汽车竞赛讯飞 -智慧餐厅技术报告 ——信号与系统课程论文
  5. jquery lt选择器与gt选择器
  6. Oracle编程入门经典 第11章 过程、函数和程序包
  7. Hiredis库的简单使用
  8. linux 运行msi文件是什么意思,查看Msi文件内容
  9. PMP之项目风险管理---实施定性风险分析
  10. 继承(instanceof :比较运算符;不仅运行父类方法,也运行子类独有的方法)
  11. flask-02-简单认识
  12. spring底层原理
  13. 光伏机器人最前线_送水、送药、送餐!哈市这些地方率先用上AI配送机器人(视频)...
  14. Go 1.8中值得关注的几个变化
  15. Collection与Arrays
  16. mac安装搜狗输入法
  17. [Frank kelly] 经济学理论对TCP的收敛性和公平性做出分析,从理论上论证了TCP在互联网环境下的稳定性和有效性
  18. 五星大饭店完整剧情,五星大饭店(完整集数)在线观看
  19. bzoj 1127 [POI2008]KUP——思路(悬线法)
  20. GeoGebra2笔记:二维或三维画图

热门文章

  1. 18、Windows API 图形用户界面(2)
  2. LC-779 语法中的第k个字符
  3. BZOJ.1312.[Neerc2006]Hard Life(分数规划 最大权闭合子图)
  4. NOIp2018集训test-9-17(pm)
  5. zjoi2018day1 游记
  6. D3之svg transform 与 css3 transform 区别与联系
  7. 数据结构与算法--图的概念
  8. 201521123097《Java程序设计》第五周学习总结
  9. 博客园博客转至个人网站博客声明
  10. 使用BIND安装智能DNS服务器(三)---添加view和acl配置