django-装饰器实现PV统计
1.models层建立统计表####
# 每日访问量统计
class Statistics(models.Model):
pv = models.IntegerField(default=0)
uv = models.IntegerField(default=0)
date = models.CharField(max_length=200)
class Meta:
verbose_name = '网站统计信息'
verbose_name_plural = '网站统计信息'
def __unicode__(self):
return self.date
2.decorator.py建立装饰器函数####
实现每次调用view试图函数前数据库字段先自加1
#!/usr/bin/env python
# -*- coding=utf-8 -*-
##################################
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=0,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试图导入装饰器函数,并调用####
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 HttpResponse('123456')
#logger.error("level info test")
return render_to_response('app/pages/index.html', ret)
django-装饰器实现PV统计的更多相关文章
- django (装饰器,母版继承,自定义,request对象,response对象)
1. 装饰器 1. def wrapper(fn): def inner(*args,**kwargs): 执行被装饰函数之前的操作 ret = fn(*args,** ...
- Python - Django - 装饰器版的登陆校验
urls.py: from django.conf.urls import url from app01 import views urlpatterns = [ url(r'^login/', vi ...
- 详解Django中六个常用的自定义装饰器
装饰器作用 decorator是当今最流行的设计模式之一,很多使用它的人并不知道它是一种设计模式.这种模式有什么特别之处? 有兴趣可以看看Python Wiki上例子,使用它可以很方便地修改对象行为, ...
- Django中六个常用的自定义装饰器
装饰器作用 decorator是当今最流行的设计模式之一,很多使用它的人并不知道它是一种设计模式.这种模式有什么特别之处? 有兴趣可以看看Python Wiki上例子,使用它可以很方便地修改对象行为, ...
- python使用装饰器@函数式化django开发
django是一个python web开发的框架.作为一个框架MVC的架构已经实现起来了.但是编码的时候你经常要进行进一步的抽象. AOP是一种称为面向切面的开发思想,意思是将部分功能代码在运行时动态 ...
- [原创]django+ldap实现单点登录(装饰器和缓存)
前言 参考本系列之前的文章,我们已经搭建了ldap并且可以通过django来操作ldap了,剩下的就是下游系统的接入了,现在的应用场景,我是分了2个层次,第一层次是统一认证,保证各个系统通过ldap来 ...
- python django 自定义 装饰器
# -*-coding:utf-8-*- __author__ = "GILANG (pleasurelong@foxmail.com)" """ d ...
- Django(五)母版继承、Cookie、视图装饰器等
大纲 一.内容回顾 补充:默认值 补充:命名空间 二.模板语言 1.母版继承 2.include 3.自定义simple_tag 三.Cookie Cookie 使用总结 四.视图 1.获取用户请求相 ...
- 给django视图类添加装饰器
要将login_required装饰到view class的dispatch方法上, 因为dispatch方法为类方法,不是单个的函数,所以需要将装饰函数的装饰器 login_required转化为装 ...
随机推荐
- jdk源码->集合->ConcurrentHashMap
类的属性 public class ConcurrentHashMap<K,V> extends AbstractMap<K,V> implements ConcurrentM ...
- JAVA中pdf转图片的方法
JAVA中实现pdf转图片可以通过第三方提供的架包,这里介绍几种常用的,可以根据自身需求选择使用. 一.icepdf.有收费版和开源版,几种方法里最推荐的.转换的效果比较好,能识别我手头文件中的中文, ...
- 解决ubuntu系统root用户下Chrome无法启动问题
由于ubuntu16.04系统自带的是Firefox浏览器,需要安装Chrome浏览器,但是在root用户下安装后发现,Chrome无法正常启动.安装及问题解决具体如下: 1. ubuntu上Chro ...
- 学会用git真的很重要
一.首先,作为一名开发人员,目前个人菜鸟一个,觉得有个仓库来管理好自己的项目是真的很重要,而目前个人认为在git上面管理自己的项目是真的很不错的推荐,接下来给大家介绍一下如何使用git上传.管理自己的 ...
- VMware10不能安装64位(linux)系统,提示此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态
今天下载VM10准备安装Ubuntu14.04,一如既往的进行安装,突然发现出现了问题:此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态,具体如图: 如图中提示可重启电脑进入B ...
- AC 自动机 模板
简单版 #include <iostream> #include <cstdio> #include <algorithm> #include <cstrin ...
- C 洛谷 P3599 Koishi Loves Construction [构造 打表观察]
题目描述 Koishi决定走出幻想乡成为数学大师! Flandre听说她数学学的很好,就给Koishi出了这样一道构造题: Task1:试判断能否构造并构造一个长度为的的排列,满足其个前缀和在模的意义 ...
- 读论文系列:Object Detection SPP-net
本文为您解读SPP-net: Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition Motivat ...
- ssh: Could not resolve hostname git.*****-inc.com : Temporary failure in name resolution fatal: The remote end hung up unexpectedly
问题出现的情景:使用git pull拉取开发的代码到测试服务器,报错: ssh: Could not resolve hostname git.****-inc.com : Temporary fai ...
- 一个很好的MySQL在线学习平台
一个很好的MySQL在线学习平台 https://www.techonthenet.com/sql/