2

I'm making a simple BBS application in Django and I want it so that whenever someone sees a post, the number of views on that post (post_view_no) is increased.

At the moment, I face two difficulties:

  • I need to limit the increase in post_view_no so that one user can only increase it once regardless of how many times the user refreshes/clicks on the post.

  • I also need to be able to track the users that are not logged in.

Regards to the first issue, it seems pretty easy as long as I create a model called 'View' and check the db but I have a feeling this may be an overkill.

In terms of second issue, all I can think of is using cookies / IP address to track the users but IP is hardly unique and I cannot figure out how to use cookies

I believe this is a common feature on forum/bbs solutions but google search only turned up with plugins or 'dumb' solutions that increase the view each time the post is viewed.

What would be the best way to go about this?

Dan
  • 369
  • 6
  • 20
  • 1
    Are you using django sessions? It will take care of cookies and identifying whether a user is anonymous or not. – Amarghosh Aug 07 '14 at 09:57
  • IMHO the question is too broad, given that you're implementing your own BBS application – Don Aug 07 '14 at 09:58
  • @Amarghosh I'm not sure if I am but I did implement the default django User app. So if I use sessions, I would be able to check that if this user (signed in / anonymous users) has seen this post before? If so, where would I store this kind of info? – Dan Aug 07 '14 at 10:08

2 Answers2

3

I think you can do both things via cookies. For example, when user visits a page, you can

  1. Check if they have “viewed_post_%s” (where %s is post ID) key set in their session.

  2. If they have, do nothing. If they don't, increase view_count numeric field of your corresponding Post object by one, and set the key (cookie) “viewed_post_%s” in their session (so that it won't count in future).

This would work with both anonymous and registered users, however by clearing cookies or setting up browser to reject them user can game the view count.

Now using cookies (sessions) with Django is quite easy: to set a value for current user, you just invoke something like

request.session['viewed_post_%s' % post.id] = True

in your view, and done. (Check the docs, and especially examples.)

Disclaimer: this is off the top of my head, I haven't done this personally, usually when there's a need to do some page view / activity tracking (so that you see what drives more traffic to your website, when users are more active, etc.) then there's a point in using a specialized system (e.g., Google Analytics, StatsD). But for some specific use case, or as an exercise, this should work.

Anton Strogonoff
  • 32,294
  • 8
  • 53
  • 61
1

Just to offer a secondary solution, which I think would work but is also prone to gaming (if coming by proxy or different devices). I haven't tried this either but I think it should work and wouldn't require to think about cookies, plus you aggregate some extra data which is noice.

I would make a model called TrackedPosts.

class TrackedPosts(models.Model):
    post = models.ForeignKey(Post)
    ip = models.CharField(max_length=16) #only accounting for ipv4
    user = models.ForeignKey(User) #if you want to track logged in or anonymous

Then when you view a post, you would take the requests ip.

def my_post_view(request, post_id):
    #you could check for logged in users as well.
    tracked_post, created = TrackedPost.objects.get_or_create(post__pk=id, ip=request.ip, user=request.user) #note, not actual api
    if created:
        tracked_post.post.count += 1
        tracked_post.post.save()
    return render_to_response('')
Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92
  • How can I use this to display a blog post view in that very blog page? I don't really get the idea. I want to get page view count and my site doesnt include login users and how is it different comparing to [this answer](https://stackoverflow.com/a/1603719/2263683)? – Ghasem Oct 25 '18 at 06:43