在我的views.py中設置查看計數器功能的最佳方法是什麼?Django查看計數器
我在Django文檔中找到了F()表達式,但是如何使它工作或者有更好的想法?
預先感謝您
def watch_video(request, slug):
video = get_object_or_404(Video, slug=slug)
template = "single_video.html"
#if this function will run , + 1 in Video.views ?
return render(request,template,{"video":video})
我的模型:
class Video(models.Model):
video_id = models.CharField(max_length=150)
title = models.CharField(max_length=150)
slug = AutoSlugField(populate_from="title")
description = models.TextField(blank=True)
views = models.PositiveIntegerField(default=0)
likes = models.PositiveIntegerField(default=0)
category = models.ForeignKey("VideoCategory")
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
tags = models.ManyToManyField("Tag")
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("watch", kwargs={"slug" : self.slug})
您不僅僅希望在來自同一IP的每次訪問時增加計數器。您需要跟蹤IP,以便如果用戶一次又一次從相同的IP訪問計數器,則不應遞增。這裏有一些軟件包,比如[django-hitcount](https://github.com/thornomad/django-hitcount/)和[django-visits](https://bitbucket.org/jespino/django-visits) – 2015-03-03 13:35:02