How to track number of hits/views for chosen objects in Django | DPS #2

How to track number of hits/views for chosen objects in Django | DPS #2

What's up DEVs? Welcome to the second post of Django Packages Series. In this quick tutorial we are going to learn how to track number of views/hits for specific objects.

Tracking is really useful functionality. When user enters your application, number of views can attract user to click specific posts and read it. Also it's good to show users most popular posts.

I saw some solutions on internet related with this topic. Some of developers using F() expression to handle view count:

Post.objects.filter(pk=post.pk).update(views=F('views') + 1)

Bad Approach!

By using F() and update views every time when user clicks on object is not good solution to create view counter. So, it will not count clicks properly, views will increase every time when same user clicks to same post.

Let me introduce you django-hitcount. Django-Hitcount allows you to track the number of hits (views) for a particular object. This isn’t meant to be a full-fledged tracking application or a real analytic tool; it’s just a basic hit counter.

Hitcounter will detect IPs and prevent from unreal views. So, views will count once for each specific user.

Create new Django project named "blog" and create app named "posts". Then run the following command to install the package.

pip3 install django-hitcount

Add django-hitcount to your INSTALLED_APPS:

settings.py
INSTALLED_APPS = (
    ...
    'hitcount'
)

There are several strategies for using django-hitcount but in this tutorial I will show you the best way to implement it in your app. Now, let's create our models:

models.py
from django.db import models
from hitcount.models import HitCountMixin, HitCount
from django.contrib.contenttypes.fields import GenericRelation
from django.utils.encoding import python_2_unicode_compatible

@python_2_unicode_compatible
class Post(models.Model):
    title = models.CharField(max_length=100)
    description = models.TextField()
    published = models.DateField(auto_now_add=True)
    slug = models.SlugField(unique=True, max_length=100)
    hit_count_generic = GenericRelation(HitCount, object_id_field='object_pk',
     related_query_name='hit_count_generic_relation')


    def __str__(self):
        return self.title

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.title)
        return super(Post, self).save(*args, **kwargs)

You are not required to do anything specific with your models; django-hitcount relies on a GenericForeignKey to create the relationship to your model’s HitCount.

Then, let's create our views:

views.py
from django.shortcuts import render
from django.views.generic.list import ListView
from hitcount.views import HitCountDetailView
from .models import Post

class PostListView(ListView):
    model = Post
    context_object_name = 'posts'
    template_name = 'post_list.html'


class PostDetailView(HitCountDetailView):
    model = Post
    template_name = 'post_detail.html'
    context_object_name = 'post'
    slug_field = 'slug'
    # set to True to count the hit
    count_hit = True

    def get_context_data(self, **kwargs):
        context = super(PostDetailView, self).get_context_data(**kwargs)
        context.update({
        'popular_posts': Post.objects.order_by('-hit_count_generic__hits')[:3],
        })
        return context

The HitCountDetailView can be used to do the business-logic of counting the hits by setting count_hit=True. Also we can filter the most viewed posts as shown above.

Now, let's configure our urls:

urls.py
from django.contrib import admin
from django.urls import path, include
from posts.views import PostListView, PostDetailView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', PostListView.as_view(), name='posts'),
    path('<slug:slug>/', PostDetailView.as_view(), name='detail'),
    path('hitcount/', include(('hitcount.urls', 'hitcount'), namespace='hitcount')),
]

To display total views for related object we will use get_hit_count template tag.

post_list.html
{% extends 'base.html' %}
{% load hitcount_tags %}

{% block content %}
  <h2>Posts List</h2>
  <ul>
    {% for post in posts %}
        <h3 class="mt-5"><a href='{% url "detail" post.slug %}'>{{post.title}}</a></h3>
        <p class="lead">{{post.description}}</p>
        <p>{{post.published}}</p>
        <p>Views: {% get_hit_count for post %}</p>
    {% endfor %}
  </ul>
{% endblock %}

post_detail.html
{% extends 'base.html' %}
{% load hitcount_tags %}

{% block content %}
  <h2>{{post.title}}</h2>
  <ul>
        <p class="lead">{{post.description}}</p>
        <p>Published: {{post.published}}</p>
        <p>Views: {% get_hit_count for post %}</p>
  </ul>

  <h3>Popular Posts</h3>
  {% for p in popular_posts %}
  <p>{{p.title}}</p>
  {% endfor %}
{% endblock %}

You can download or clone the project on my GitHub

Django-Hitcount-Tutorial

That's it! Make sure you are following me on social media and please support me by buying me a coffee☕ so I can upload more tutorials like this. See you in next post DEVs!

Reverse Python Instagram Twitter