python-pv-uv

1. Models layer to create a statistical table

#Daily traffic statistics 
class Statistics(models.Model):
    pv = models.IntegerField(default=0)
    uv = models.IntegerField(default=0)
    date = models.CharField(max_length=200)
    class Meta:
        verbose_name = ' Site Statistics ' 
        verbose_name_plural = ' Site Statistics '
    def__unicode__(self):
        return self.date

2.decorator.py creates a decorator function

The database field is incremented by 1 before each call to the view attempt function is implemented

from models import Statistics
import time
def pvCount(func):
    def wrapper(request, *args, **kwargs):
        dateObj_list = Statistics.objects.filter(date=time.strftime('%Y-%m-%d'))
        count = dateObj_list.count()
        if count == 0:
            Statistics.objects.create(pv=1,uv=1,date=str(time.strftime('%Y-%m-%d')))
        else:
            todayObj = Statistics.objects.get(date=str(time.strftime('%Y-%m-%d')))
            todayObj.pv += 1
            todayObj.save()
        return func(request, *args, **kwargs)
    return wrapper

3.view tries to import the decorator function and calls

from decorator import pvCount
@pvCount
def index(request):
    count = models.NewMachine.objects.all().count()
    phycount = models.PhysicalHost.objects.all().count()
    ret = dict()
    ret['count'] = count
    ret['phycount'] = phycount
    return render_to_response('index.html', ret)

 

 

 

 

1. Models layer to create a statistical table

#Daily traffic statistics 
class Statistics(models.Model):
    pv = models.IntegerField(default=0)
    uv = models.IntegerField(default=0)
    date = models.CharField(max_length=200)
    class Meta:
        verbose_name = ' Site Statistics ' 
        verbose_name_plural = ' Site Statistics '
    def__unicode__(self):
        return self.date

2.decorator.py creates a decorator function

The database field is incremented by 1 before each call to the view attempt function is implemented

from models import Statistics
import time
def pvCount(func):
    def wrapper(request, *args, **kwargs):
        dateObj_list = Statistics.objects.filter(date=time.strftime('%Y-%m-%d'))
        count = dateObj_list.count()
        if count == 0:
            Statistics.objects.create(pv=1,uv=1,date=str(time.strftime('%Y-%m-%d')))
        else:
            todayObj = Statistics.objects.get(date=str(time.strftime('%Y-%m-%d')))
            todayObj.pv += 1
            todayObj.save()
        return func(request, *args, **kwargs)
    return wrapper

3.view tries to import the decorator function and calls

from decorator import pvCount
@pvCount
def index(request):
    count = models.NewMachine.objects.all().count()
    phycount = models.PhysicalHost.objects.all().count()
    ret = dict()
    ret['count'] = count
    ret['phycount'] = phycount
    return render_to_response('index.html', ret)

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324693702&siteId=291194637