Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a page view count for django detail view?

I am performing a Blog application using Django. I want to track the count of the page view whenever a user sees a particular blog, whether it is a registered user or non-registered users. And I also want to display the most viewed blogs according to the view count.

Can anyone help me out in this? Thank you.

like image 508
Niladry Kar Avatar asked Sep 20 '18 12:09

Niladry Kar


3 Answers

models.py

class Blog(models.Model):
    #fields you need
    blog_views=models.IntegerField(default=0)

views.py

def blog_post(request,post_id):
    #your code
    blog_object=Blog.objects.get(id=post_id)
    blog_object.blog_views=blog_object.blog_views+1
    blog_object.save()
    #your code

This will simply count each blog visits. This will also count multiple views by a single user.

like image 163
Abhijith K Avatar answered Oct 20 '22 17:10

Abhijith K


We can count views using IPAdress by creating a table for post views in the database.

In models.py

from django.contrib.auth.models import User
class PostViews(models.Model):
    IPAddres= models.GenericIPAddressField(default="45.243.82.169")
    post = models.ForeignKey('Post', on_delete=models.CASCADE)

    def __str__(self):
        return '{0} in {1} post'.format(self.IPAddres,self.post.title)

Then, make it a property to the Post class like that.

models.py for example:

class Post(models.Model):
    title = models.CharField(max_length=100, unique= True)
    slug= models.SlugField(blank=True, null=True, unique=True)
    @property
    def views_count(self):
        return PostViews.objects.filter(post=self).count()

you can read about property here then

In views.py

from .models import PostViews

def PostsDetailsView(request,slug):
    post_details=Post.objects.get(slug=slug)
    def get_client_ip(request):
        x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
        if x_forwarded_for:
            ip = x_forwarded_for.split(',')[0]
        else:
            ip = request.META.get('REMOTE_ADDR')
        return ip

    PostViews.objects.get_or_create(user=request.user, post=post_details)

So this function ensures that if this IPAdress has seen this post it will do nothing if he sees the post for the first time it will create an object in the database and count it as a view. You can read more about IPAdress here. Don't forget to make migrations, migrate and register PostViews class in admin.py

like image 43
Mohab Mohamed Gamal Avatar answered Oct 20 '22 15:10

Mohab Mohamed Gamal


  1. Add IntegerField to your Model for storing count of views.
  2. Change your view so, it will increase the count by adding one to previous count when view gets request.

models.py:

blog_view = models.IntegerField(default=0)

views.py:

class BlogView(DetailView):
    model = Blog
    def get_object(self):
        obj = super().get_object()
        obj.blog_view += 1
        obj.save()
        return obj
like image 34
Adi Nishad Avatar answered Oct 20 '22 17:10

Adi Nishad