如何计算django中博客的观点
我正在尝试计算我的Django项目中每个博客中的所有视图。最好的方法是什么? (赦免错误)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我正在尝试计算我的Django项目中每个博客中的所有视图。最好的方法是什么? (赦免错误)
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(1)
假设您想在每次观众查看帖子时都可以更新帖子计数,这是您可以采用的一种方法。
您可以在
Post Model
上添加整数字段
。查看该帖子后,可以更新帖子上的此字段。您还可以在
发布模型
上添加方法
,该模型每当被调用时都会专门更新视图计数,该视图计数可以允许从hmtl
模板本身(不推荐),以及views
(是否CBV或FBV)。在
models.py.py
文件中:在 app
urls.py
文件中:
views.pys.pys.py
file:Here's an approach you could take, assuming that you want to update a post view count each time that post has been viewed by a viewer.
You can add an
integer field
on thePost model
. This field on a post can be updated when that post has been viewed.You can also add a
method
on thePost model
that will specifically update the view count whenever it is called, which can allow the post view count to be updated from thehmtl
template itself (not recommended), as well as in theviews
(whether cbv or fbv).Within the
models.py
file:Within the app
urls.py
file:Within the
views.py
file: