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_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.


  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Django中,可以使用权限系统来控制用户对不同页面的访问权限。权限系统基于用户和用户组的概念来进行权限管理。 首先,需要在数据库中创建权限记录。可以使用Django提供的`manage.py`命令来创建权限记录,也可以在Django Admin后台手动创建。例如,可以创建一个名为"访问页面A"的权限记录。 接下来,可以在视图函数或类中使用`@permission_required`装饰器来限制页面的访问权限。通过该装饰器,只有拥有特定权限的用户才能访问被装饰的视图。例如,可以在视图函数中加上`@permission_required('app_name.访问页面A')`。 此外,还可以在模板文件中使用`{% if user.has_perm %}`来判断用户是否拥有特定权限,从而动态展示页面上的内容。例如,可以在模板中使用`{% if user.has_perm 'app_name.访问页面A' %} 显示页面A的内容 {% endif %}`。 如果要限制整个应用中的所有视图都需要特定权限才能访问,可以在项目的URL配置中使用`login_required`装饰器和`PermissionRequiredMixin`类。例如,可以在`urls.py`文件中对整个应用添加如下代码: ``` from django.contrib.auth.decorators import login_required from django.contrib.auth.mixins import PermissionRequiredMixin urlpatterns = [ path('app_name/', login_required(PermissionRequiredMixin.as_view( permission_required='app_name.访问页面A')), name='app_name'), # ... ] ``` 以上是使用Django权限系统来控制不同页面的访问权限的基本方法。通过创建权限记录、使用装饰器和在模板中判断权限,可以实现在不同页面中控制用户的访问权限。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不太灵光的程序员

有用的话可以请博主喝杯咖啡续命

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值