Django:如何计算观看的人数

编程入门 行业动态 更新时间:2024-10-08 22:51:41
本文介绍了Django:如何计算观看的人数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在Django中制作一个简单的BBS应用程序,我想要这样,每当有人看到某条信息时,该信息(post_view_no)的浏览次数就会增加。

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:

  • 我需要限制post_view_no的增加,只需增加一次,无论用户刷新/点击信息的次数。

  • 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.

对于第一个问题,看起来很容易,只要我创建一个名为'View'并检查db,但我有一种感觉这可能是一个过度。

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.

在第二个问题,我可以想到的是使用cookies / IP地址来跟踪

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

我相信这是一个常见的功能,论坛/ bbs解决方案,但google搜索只出现在插件

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?

推荐答案

我想你可以通过cookie做这两件事。例如,当用户访问某个网页时,您可以

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

  • 检查他们是否有seen_post_%s

  • Check if they have "viewed_post_%s" (where %s is post ID) key set in their session.

    如果有,则不执行任何操作。如果没有,请将对应的 Post 对象的 view_count 数字字段增加一,并设置键)

    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).

    现在使用Django使用Cookie(工作阶段)时,很容易:为当前用户设置一个值,只需调用

    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.)

    免责声明:这是我的头,我没有做这个个人,通常当有需要做一些页面查看/活动跟踪(以便你看到什么驱动更多的流量到您的网站,当用户更活跃等),那么在使用一个专门的系统(例如,Google Analytics,StatsD)有一点。但对于一些特定的用例,或作为一个练习,这应该工作。

    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.

  • 更多推荐

    Django:如何计算观看的人数

    本文发布于:2023-10-23 02:44:00,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1519511.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:人数   Django

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!