<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>damontimm.com &#187; django</title>
	<atom:link href="http://blog.damontimm.com/tag/django/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.damontimm.com</link>
	<description>Where I go to remember what I did</description>
	<lastBuildDate>Tue, 17 Aug 2010 13:07:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>django-hitcount: simple app to count hits/views for an object</title>
		<link>http://blog.damontimm.com/django-hitcount-app-count-hits-views/</link>
		<comments>http://blog.damontimm.com/django-hitcount-app-count-hits-views/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 01:10:38 +0000</pubDate>
		<dc:creator>Damon</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.damontimm.com/?p=201</guid>
		<description><![CDATA[django-hitcount: a simply django application that allows you to count hits/views on a per object basis. This app came about as an answer to my own question at stackoverflow.com. Am hoping that others will find it useful. This isn&#8217;t meant to be a full-fledged tracking application (see django-tracking) or a real analytic tool (try Google [...]]]></description>
			<content:encoded><![CDATA[<p><strong>django-hitcount</strong>: a simply django application that allows you to count hits/views on a per object basis.  This app came about as an answer to <a href="http://stackoverflow.com/questions/1603340/track-the-number-of-page-views-or-hits-of-an-object">my own question</a> at stackoverflow.com.  Am hoping that others will find it useful.</p>
<p><span id="more-201"></span></p>
<p>This isn&#8217;t meant to be a full-fledged tracking application (see django-tracking) or a real analytic tool (try Google Analytics); rather, it&#8217;s meant to simply count the number of hits/view on an object-per-object basis.  </p>
<h2>How to install:</h2>
<p>I find that the easiest way to work with django apps is to symbolically link them to my <code>site-packages</code> directory.  It&#8217;s easier to update the apps with svn, git, or hg than it is to manually download the files and install by hand.</p>
<p>For me, this is what it looks like (you can cut and paste to make it easy):</p>
<ul class="terminal">
<li><code>cd ~/src</code></li>
<li><code>git clone git://github.com/thornomad/django-hitcount.git</code></li>
<li><code>sudo ln -s `pwd`/django-hitcount/hitcount `python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"`/hitcount</code></li>
</ul>
<p>Test it works by loading python and checking the version (should not get an error):</p>
<pre class="brush: python;">
>>> import hitcount
>>> hitcount.__version__
'0.1 alpha'
>>></pre>
<h2>Adding to your django project:</h2>
<p>Add the <code>hitcount</code> app to your <code>INSTALLED_APPS</code> tuple and run <code>syncdb</code>.  </p>
<p>There are two additional settings you can add to your <code>settings.py</code> file:</p>
<pre class="brush: python;">HITCOUNT_KEEP_HIT_ACTIVE = { 'days': 7 }
HITCOUNT_HITS_PER_IP_LIMIT = 0
HITCOUNT_EXCLUDE_USER_GROUP = ( 'Editor', )</pre>
<p><code><strong>HITCOUNT_KEEP_HIT_ACTIVE</strong></code>: is the number of days, weeks, months, hours, etc (timedelta kwargs), that an Hit is kept &#8216;active&#8217;.  If a Hit is &#8216;active&#8217; a repeat viewing will not be counted.  After the active period ends, however, a new Hit will be recorded.  You can decide how long you want this period to last &#8230;</p>
<p><code><strong>HITCOUNT_HITS_PER_IP_LIMIT</strong></code>: limit the number of &#8216;active&#8217; hits from a single IP address.  <code>0</code> means that it is unlimited.  You may want to set this, or not.  </p>
<p><code><strong>HITCOUNT_EXCLUDE_USER_GROUP</strong></code>: don&#8217;t count any hits from certain logged in users.  In the example above, I don&#8217;t want any of my editors inflating the total Hit count.</p>
<h2>Adding to your <code>urls.py</code>:</h2>
<p>You need to add one line to your <code>urls.py</code> file.  </p>
<p>You can have this url, itself, point to anywhere you like, but you need to keep the <code>name='hitcount_update_ajax'</code> constant.  </p>
<pre class="brush: python; gutter: true;">from django.conf.urls.defaults import *
from django.views.generic.list_detail import object_detail
from hitcount.views import update_hit_count_ajax

urlpatterns = patterns('',
    url(r'^ajax/hit/$', # you can change this url if you would like
        update_hit_count_ajax,
        name='hitcount_update_ajax'), # keep this name the same

    # other views, for example my object view is:

    url(r'^/video/(?P&lt;object_id>\d+)$', object_detail,
        {   'queryset': Video.objects.all(),
            'template_name': "video/view.html"},
            name='video_detail_view'),

)</pre>
<h2>Edit your templates</h2>
<p>Add the javascript to your <code>object_detail</code> templates (or any template that handles a single object) so that our hit counter is called after the document loads.  </p>
<p>Here is what my <code>head</code> includes:</p>
<pre class="brush: html;">{% load hitcount_tags %}
&lt;script src="/media/js/jquery-latest.js" type="text/javascript">&lt;/script>
&lt;script type="text/javascript">&lt;!--
    $(document).ready(function() {
        {% get_hit_count_javascript for object %}
    });
-->&lt;/script></pre>
<p>When the template is rendered, it should turn into something like this:</p>
<pre class="brush: js;">
$(document).ready(function() {

$.post( '/ajax/hit/',
	{ hitcount_pk : '3' },
	function(data, status) {
		if (data.status == 'error') {
			// do something for error?
		}
	},
	'json');

});</pre>
<h2>Display the hits!</h2>
<p>The most exciting part, is actually displaying your hits.  There are four different ways to do it:</p>
<pre class="brush: text;">    - 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] %}</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.damontimm.com/django-hitcount-app-count-hits-views/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
