如何计算django中博客的观点

发布于 2025-02-03 09:48:49 字数 52 浏览 3 评论 0 原文

我正在尝试计算我的Django项目中每个博客中的所有视图。最好的方法是什么? (赦免错误)

I am trying to count and show all views in every blog in my django project. What is the best way to do that? (Pardon mistakes)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

总以为 2025-02-10 09:48:49

假设您想在每次观众查看帖子时都可以更新帖子计数,这是您可以采用的一种方法。

您可以在 Post Model 上添加整数字段。查看该帖子后,可以更新帖子上的此字段。

您还可以在发布模型上添加方法,该模型每当被调用时都会专门更新视图计数,该视图计数可以允许从 hmtl 模板本身(不推荐),以及 views (是否CBV或FBV)

models.py.py 文件中:

class Post(models.Model):
     ...
     views = models.IntegerField(default=0) # Upon creation the views will be 0
     ...

    # You can have
    def get_absolute_url(self):
         return reverse('post-details', kwargs={"id": self.id})

    # An alternative to use to update the view count 
    def update_views(self, *args, **kwargs):
         self.views = self.views + 1
         super(Post, self).save(*args, **kwargs)

app urls.py 文件

from app.views import PostDetailsView, post_details

urlpatterns = [
    ...
    # Using either of the following
    path('post-details/<int:id>/', PostDetailsView.as_view(), name='post-details'), # class-based view
    path('post-details/<int:id>/', post_details, name='post-details'), # function-based view 
    ...
]

中: views.pys.pys.py file:

# Class based view
class PostDetailView(DetailView):
     context_object_name = 'post'
     template_name = 'post-details.html'

     # Overriding the class get_object method
     def get_object(self):
          post_id = self.kwargs.get('id')
          post = get_object_or_404(Post, id=post_id)

          # Update the view count on each visit to this post.
          if post:
               post.views = post.views + 1
               post.save()
   
               # Or
               post.update_views()

          return post

    # You might have other methods in here if necessary
    ...

# Function based view
def post_detail(request, id):
     post = get_object_or_404(Post, id=id)

     # Update the view count on each visit to this post.
      if post:
           post.views = post.views + 1
           post.save()

           # Or
           post.update_views()

     ...

     context = {
          'post': post,
           ...
     }

     return render(request, "post-details.html", context)

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 the Post model. This field on a post can be updated when that post has been viewed.

You can also add a method on the Post model that will specifically update the view count whenever it is called, which can allow the post view count to be updated from the hmtl template itself (not recommended), as well as in the views (whether cbv or fbv).

Within the models.py file:

class Post(models.Model):
     ...
     views = models.IntegerField(default=0) # Upon creation the views will be 0
     ...

    # You can have
    def get_absolute_url(self):
         return reverse('post-details', kwargs={"id": self.id})

    # An alternative to use to update the view count 
    def update_views(self, *args, **kwargs):
         self.views = self.views + 1
         super(Post, self).save(*args, **kwargs)

Within the app urls.py file:

from app.views import PostDetailsView, post_details

urlpatterns = [
    ...
    # Using either of the following
    path('post-details/<int:id>/', PostDetailsView.as_view(), name='post-details'), # class-based view
    path('post-details/<int:id>/', post_details, name='post-details'), # function-based view 
    ...
]

Within the views.py file:

# Class based view
class PostDetailView(DetailView):
     context_object_name = 'post'
     template_name = 'post-details.html'

     # Overriding the class get_object method
     def get_object(self):
          post_id = self.kwargs.get('id')
          post = get_object_or_404(Post, id=post_id)

          # Update the view count on each visit to this post.
          if post:
               post.views = post.views + 1
               post.save()
   
               # Or
               post.update_views()

          return post

    # You might have other methods in here if necessary
    ...

# Function based view
def post_detail(request, id):
     post = get_object_or_404(Post, id=id)

     # Update the view count on each visit to this post.
      if post:
           post.views = post.views + 1
           post.save()

           # Or
           post.update_views()

     ...

     context = {
          'post': post,
           ...
     }

     return render(request, "post-details.html", context)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文