如何为 django 详细信息视图添加页面查看计数? [英] How to add a page view count for django detail view?

查看:20
本文介绍了如何为 django 详细信息视图添加页面查看计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 django 执行博客应用程序..

I am performing a Blog application using django..

每当用户看到特定博客时,无论是注册用户还是非注册用户,我都想跟踪页面查看次数...

I want to track the count of the page view whenever a user see a particular blog whether it is a registered user or non-registered users...

并且还想根据查看次数显示查看次数最多的博客..

And also want to display the most viewed blogs according to the view count..

谁能帮我解决这个问题..

Can anyone help me out in this..

谢谢

推荐答案

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.

这篇关于如何为 django 详细信息视图添加页面查看计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆