http://blog.csdn.net/pipisorry/article/details/47396311
Here is a simple page click Statistics, model reading volume statistics, user access statistics in the template method
Implementation of a simple template page count
Settings in the template:
< Li > You are the {{count}} visitors to this site friend </li><Li> Access time: {{time}}</li>
In view.py
defGetTime ():#Get current Time Import Timereturntime.ctime ()defGetCount ():#Get access timesCountfile = open ('Count.dat','A +')#Open Record Count files in read-write formCounttext =Countfile.read ()Try: Count= Int (counttext) +1except: Count= 1countfile.seek (0) countfile.truncate ()#Empty FileCountfile.write (str (count))#re-write new trafficCountfile.flush () countfile.close ( )returnCountdefMyhelloworld (Request):Time =getTime () Count=GetCount () para= {"Count": Count," Time": Time}...
This will be called every time the access isMyhelloworld function, reading the count value and +1 operation
[Use a template to make a site access counter]
http://blog.csdn.net/pipisorry/article/details/47396311
The counter implementation of the Model object
Django Hit counter application This tracks the number of Hits/views for chosen objects.
Hit counter is used to count the number of accesses to the model object.
Install Django-hitcount:
Pip Install Django-hitcount
settings.py
Add Django-hitcount to your INSTALLED_APPS
, enable SESSION_SAVE_EVERY_REQUEST
:
# settings.pyInstalled_apps = (... ') Hitcount ')# needed for Django-hitcount to function properlysession_save_every_request = True
urls.py
urls.py中加入
# urls.pyurlpatterns = 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 it works Out-of-the-box to record the to an Hits
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.
Add loading Hitcount tags at the beginning of the template where you need it
{% 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 can Use a, template variable to inject as, see fit{% get_hit_count_js_variables for object as Hitcount%}({Hitcoun T.ajax_url}}{{hitcount.pk}}
Displaying hit Information
You can retrieve the number of hits to an object many different ways:
# Return Total hits for the object:{% get_hit_count for [object]%}# get total hits for a 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 [ob Ject] within ["days=1,minutes=30"]%}# Get Total hits in a object over a certain time period as a variable:{% Get_hit_co UNT for [object] within ["days=1,minutes=30"] as [var]%}
[Installation and Usage]
[Example Project]
[Django-hitcount]
http://blog.csdn.net/pipisorry/article/details/47396311
User traffic statistics for pages
django-tracking
Keeps track of visitors to django-powered Web sites. It also offers basic blacklisting capabilities.
Installationdjango-tracking
Pip Install django-tracking
Note: Error: No module named listeners
Configuration
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 ', ...)
Run manage.py syncdb
. This creates a few tables in your database the arenecessary for operation.
Depending on how do you wish to use this application and you have a few options:
Visitor Tracking
Add to tracking.middleware.VisitorTrackingMiddleware
your MIDDLEWARE_CLASSES
in settings.py
. It must AuthenticationMiddleware
is underneath the, so, request.user
exists.
Automatic Visitor clean-up
If you want to has Django automatically clean past visitor information Outyour database, put in tracking.middleware.VisitorCleanUpMiddleware
your MIDDLEWARE_CLASSES
.
IP Banning
Add to tracking.middleware.BannedIPMiddleware
your MIDDLEWARE_CLASSES
in settings.py
. I would recommend making this is the very first item in so your banned users does not has to MIDDLEWARE_CLASSES
drill through anyother Midd Leware before Django realizes they don ' t belong on your site.
Visitors on Page (template tag)
Make sure, that's django.core.context_processors.request
somewhere in your TEMPLATE_CONTEXT_PROCESSORS
tuple. This context processor makes the request
object accessible to your templates. This application uses, the object to determine, the request
user is looking at a templatetag.
Active Visitors Map
If you ' re interested in seeing where your visitors is at a given point Intime, you might enjoy the active visitor map FEA Ture. Is sure you has 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 your GeoIP.dat
or GeoIPCity.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 if 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 than Geoip_standard
, but lessmemory usage than geoip_memory_cache
– useful for larger Databasessuch As GeoIP Organization and GeoIP city. Note, for GeoIP country,region and Netspeed databases, Geoip_index_cache
are equivalent to geoip_memory _cache
. default
DEFAULT_TRACKING_TEMPLATE
: The template to use when generating thevisitor map. Defaults to tracking/visitor_map.html
.
When that's done, you should is able to go to the /tracking/map/
your site (replacing with tracking
whatever prefix y chose Our urlconf,obviously). The default template relies upon jQuery for it awesomeness, Butyou ' re free to use whatever-like.
Usage
To display the number of active users there is in one of your templates, makesure and somewhere in your Templat {% load tracking_tags %}
E 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 is 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 being tracked, you could define alist of prefixes in your using the settings.py
. Forexample, if you didn ' t want visits to the sections of /family/
your Website,set NO_TRACKING_PREFIXES
to [‘/family/‘]
.
If you don't want to count certain user-agents, such as Yahoo! ' s slurp andgoogle ' s Googlebot, you could 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 won't be tracked.
By default, active users include any visitors within of the last minutes. Ifyou would like-to-override that setting, just set to TRACKING_TIMEOUT
howevermany minutes you want in your settings.py
.
For automatic visitor clean-up, any records older than, hours is removed bydefault. If you would like to override that setting, set to TRACKING_CLEANUP_TIMEOUT
however many hours you want in your settings.py
.
[
django-tracking] from:http://blog.csdn.net/pipisorry/article/details/47396311
Ref: Implement a high-performance unread message counter in Django
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Django Project deployment-traffic statistics and page hits