python - Django hit counts Class-based views -
i'm trying youtube views:
models.py class video(models.model): title = models.charfield(max_length=100) embed = models.textfield created_at = models.datetimefield(auto_now_add=true,editable=false) updated_at = models.datetimefield(auto_add=true,editable=false) visit_count = models.integerfield(default=0) def add_visit(self): if self.visit_count not none: self.visit_count += 1 else: self.visit_count = 0 views.py class videodetail(detailview): model = video def get_context_data(self, **kwargs): context = super(videodetail, self).get_context_data(**kwargs) self.object.add_visit() self.object.save() return context example use: video.objects.order_by('-visit_count')
so working properly, not quite, can count limit ip.
class video(models.model): title = models.charfield(max_length=100) embed = models.textfield created_at = models.datetimefield(auto_now_add=true,editable=false) updated_at = models.datetimefield(auto_add=true,editable=false) class visit(models.model): video = models.foreignkey(video) ip = models.ipaddressfield() visit_count = models.integerfield(default=0)
how insert in views or models: visit_count,ip,video.
in view:
def get(self, req, *k, **kw) is_created, visit = visit.objects.get_or_create(this.object, ip) if is_created: this.object.add(visit) else: visit.count +=1 visit.save()
Comments
Post a Comment