点击Django页面的计数器

时间:2022-04-09 15:19:26

I want to have a page counter that displays the number of visitors who have viewed a particular page on my site. Is it possible to do this using Django?

我想要一个页面计数器,显示在我的网站上查看过特定页面的访问者数量。是否可以使用Django执行此操作?

3 个解决方案

#1


9  

There's a Django app for that problem called django-hitcount. It's easy to use, and reusable in any of your projects.

有一个名为django-hitcount的问题的Django应用程序。它易于使用,并且可以在任何项目中重复使用。

#2


9  

A "page counter" is what? A persistent piece of data which gets updated by view functions and displayed by a template.

一个“页面计数器”是什么?一段持久的数据,由视图函数更新并由模板显示。

As you are no doubt already aware, all Django things have the following parts.

毫无疑问,您已经意识到,所有Django都有以下几个部分。

  1. Model
  2. View Function
  3. Template

Model

If you want to keep the page counter in the database, you need a Django model.

如果要将页面计数器保留在数据库中,则需要Django模型。

class PageCounter( Model ):

You need to put a row into this model. Usually a "fixture" will help do this, since it's one row and you only put it in once when doing a syncdb.

您需要在此模型中添加一行。通常情况下,“fixture”会帮助实现这一点,因为它只有一行而你只需要在执行syncdb时将其放入一行。

View Function

Then you need to fetch and update the page counter in your view function.

然后,您需要在视图函数中获取并更新页面计数器。

pageCounter= PageCounter.objects.all()[0] 
pageCounter.count += 1
pageCounter.save()

Template

Now you need to provide the value to your templates so it can be displayed.

现在,您需要为模板提供值,以便可以显示它。

#3


1  

I know this is an old post but occasionally people might have the same question.

我知道这是一个老帖子,但偶尔人们可能会有同样的问题。

If you want to avoid a third party library and prevent the counter being updated at every page refresh you could do the following Mixin (building on S.Lott's answer)

如果你想避免使用第三方库并阻止计数器在每次刷新页面时更新,你可以执行以下Mixin(基于S.Lott的答案)

class BlogPostCounterMixin(object):
    def get_context_data(self, **kwargs):
        context = super(BlogPostCounterMixin, self).get_context_data(**kwargs)
        blog_post_slug = self.kwargs['slug']
        if not blog_post_slug in self.request.session:
            bp = BlogPost.objects.filter(slug=blog_post_slug).update(counter=+1)
            # Insert the slug into the session as the user has seen it
            self.request.session[blog_post_slug] = blog_post_slug
    return context

It checks if the accessed model has been stored in the session. If it has been stored in the session, it skips incrementing, else it increments the counter and adds the slug of the model to the session preventing increments for page refreshes.

它检查所访问的模型是否已存储在会话中。如果它已存储在会话中,则会跳过递增,否则它会递增计数器并将模型的slug添加到会话中,从而阻止页面刷新的增量。

Note: This is a Mixin which you require to add into your view.

注意:这是您需要添加到视图中的Mixin。

#1


9  

There's a Django app for that problem called django-hitcount. It's easy to use, and reusable in any of your projects.

有一个名为django-hitcount的问题的Django应用程序。它易于使用,并且可以在任何项目中重复使用。

#2


9  

A "page counter" is what? A persistent piece of data which gets updated by view functions and displayed by a template.

一个“页面计数器”是什么?一段持久的数据,由视图函数更新并由模板显示。

As you are no doubt already aware, all Django things have the following parts.

毫无疑问,您已经意识到,所有Django都有以下几个部分。

  1. Model
  2. View Function
  3. Template

Model

If you want to keep the page counter in the database, you need a Django model.

如果要将页面计数器保留在数据库中,则需要Django模型。

class PageCounter( Model ):

You need to put a row into this model. Usually a "fixture" will help do this, since it's one row and you only put it in once when doing a syncdb.

您需要在此模型中添加一行。通常情况下,“fixture”会帮助实现这一点,因为它只有一行而你只需要在执行syncdb时将其放入一行。

View Function

Then you need to fetch and update the page counter in your view function.

然后,您需要在视图函数中获取并更新页面计数器。

pageCounter= PageCounter.objects.all()[0] 
pageCounter.count += 1
pageCounter.save()

Template

Now you need to provide the value to your templates so it can be displayed.

现在,您需要为模板提供值,以便可以显示它。

#3


1  

I know this is an old post but occasionally people might have the same question.

我知道这是一个老帖子,但偶尔人们可能会有同样的问题。

If you want to avoid a third party library and prevent the counter being updated at every page refresh you could do the following Mixin (building on S.Lott's answer)

如果你想避免使用第三方库并阻止计数器在每次刷新页面时更新,你可以执行以下Mixin(基于S.Lott的答案)

class BlogPostCounterMixin(object):
    def get_context_data(self, **kwargs):
        context = super(BlogPostCounterMixin, self).get_context_data(**kwargs)
        blog_post_slug = self.kwargs['slug']
        if not blog_post_slug in self.request.session:
            bp = BlogPost.objects.filter(slug=blog_post_slug).update(counter=+1)
            # Insert the slug into the session as the user has seen it
            self.request.session[blog_post_slug] = blog_post_slug
    return context

It checks if the accessed model has been stored in the session. If it has been stored in the session, it skips incrementing, else it increments the counter and adds the slug of the model to the session preventing increments for page refreshes.

它检查所访问的模型是否已存储在会话中。如果它已存储在会话中,则会跳过递增,否则它会递增计数器并将模型的slug添加到会话中,从而阻止页面刷新的增量。

Note: This is a Mixin which you require to add into your view.

注意:这是您需要添加到视图中的Mixin。