Django 类视图 添加装饰器
2018-03-23 17:54
281 查看
今天优化代码的时候想到用装饰器精简函数执行的计时代码,想要把装饰器加到类视图中,在成哥的指导下,完成了
参考:
https://docs.djangoproject.com/en/1.8/topics/class-based-views/intro/#decorating-the-class
http://www.jb51.net/article/61454.htm
from functools import wraps def count_time_cost(func): @wraps(func) def wrapper(*args, **kwargs): start = datetime.datetime.now() r = func(*args, **kwargs) end = datetime.datetime.now() LOG.info("Django view function run finished! Task_message : {} Cost_time : {}".format(r.data.get("msg"), (end - start))) return r return wrapper
class VolumeQueryNew(APIView): @method_decorator(count_time_cost) def get(self, request, *args, **kwargs): zone_id = request.query_params.get("zoneid") volume_uuid = request.query_params.get("volume_uuid") or request.query_params.get("volume_id") rc = get_rc_by_udc(request) add_mark = "1" volume_filtered_info = [] try: msg = 'Get all volume info successfully!' LOG.info("volumes info is{}".format(str(volume_filtered_info))) pagination_class = APIViewPagePagination return common_page_response(volume_filtered_info, request, msg, pagination_class) except Exception as e: LOG.exception("failed to get volumes!error exception msg{}".format(e)) return common_error_response("failed to get volumes!error msg", FAILED_TO_GET_ALL_VOLUMES)
参考:
https://docs.djangoproject.com/en/1.8/topics/class-based-views/intro/#decorating-the-class
http://www.jb51.net/article/61454.htm
相关文章推荐
- Django 中针对基于类的视图添加 csrf_exempt
- Django中文官方版07-添加视图模板
- 详解Django中类视图使用装饰器的方式
- django学习 笔记---给cookie添加装饰器(FBV和CBV模式)
- Django:视图装饰器
- Django(五)母版继承、Cookie、视图装饰器等
- Django中针对基于类的视图添加csrf_exempt实例代码
- Django 中针对基于类的视图添加 csrf_exempt
- Asp.Net MVC4入门指南(3):添加一个视图
- iphone开发基础_向表视图添加子视图_第八章_P157
- 在app中添加新手引导视图(XCode4.6)
- 2. 为视图添加工具栏,实现新建,打开,删除的功能
- [django]入门教程2:视图开发-定义视图函数及配置相应URL
- 如何在Django后台添加pagedown
- Asp.Net MVC4.0 官方教程 入门指南之三--添加一个视图
- 在半透明view上添加子视图也为透明解决办法
- SAP物料主数据创建质量管理视图后添加检验类型
- MVC2 检查编辑方法并编辑视图添加简单的搜索
- Django - 通用视图
- Django学习笔记(2):Django视图和URL