Django hitcount
is a python library that allows you track the number of hits/views on a particular objects by detecting the IP address of each click to prevent unreal views this ensures views aren't counted twice.
This library was built using the Django’s class based views which provides a wrapper for Django’s generic DetailView
and allows you to process the Hit as the view is loaded.
To begin with this library you must have Python and Django running on your system, next we install the django-hitcount
using pip install django-hitcount
, after installation we add the hitcount library to our installed applications in settings.py
Next, we modify our models.py
file by importing the following line of code
from hitcount.models import HitCountMixin, HitCount
from django.contrib.contenttypes.fields import GenericRelation
this library was built using Django's class based views so for beginners who might have already began using function based view in their application should have no worry as you can use both function based views and a class based view in one application view.
So, now lets move to our views.py
and tweak it a little, but before we do that, we need to import our hitcount into our views.py
file
from hitcount.views import HitCountDetailView
The HitCountDetailView
can be used to do the business-logic of counting the hits by setting count_hit=True
.
Almost there, now we move to our HTML template and load our hitcount tags, just right above your html DOCTYPE load the hitcount tags
{% load hitcount_tags %}
Finally, we add styling to our application with this line of code
<span class="ion-ios-eye"></span> {% get_hit_count for project %} views
VOILA!!! You've successfully integrated Django's hitcount to your application
Top comments (2)
what is the main use case for this library ?
social media sites??
To track the number of views or click on a particular object...