从django的中间件直接返回请求的方法

作者:Iphone4 时间:2022-02-04 23:32:23 

实例如下所示:


#coding=utf-8
import json
import gevent
from django.http import HttpResponse
from sdsom.web.recorder import get_event_type
from sdsom.web.recorder import get_request_event_info
from sdsom.db.rpcclient import get_db_client
class RecordEventMiddleWare(object) :
def process_view(self, request, view, args, kwargs) :
etype = get_event_type(request)
if not etype :
 return None
info = get_request_event_info(request, etype)
info['status'] = "BEGIN"
try:
 get_db_client().add_event_record(info)
except :
 return HttpResponse(
  json.dumps({"susscess":0, "message":"记录事件开始到数据库出错"}),
  content_type='application/json'
  )
return None

如上代码所示,需要从django的http模块导入HttpResponse类,

然后返回的时候可以把自己想要返回的字典内容用jsondump一把(如果不dump,上一层会处理报错)。

来源:https://blog.csdn.net/computerms/article/details/50587965

标签:django,中间件,请求
0
投稿

猜你喜欢

  • 如何使用Python的Requests包实现模拟登陆

    2022-10-07 03:12:26
  • Python 中包/模块的 `import` 操作代码

    2021-07-10 02:52:20
  • Python使用filetype精确判断文件类型

    2023-07-09 12:30:12
  • Mysql limit 优化,百万至千万级快速分页 复合索引的引用并应用于轻量级框架

    2024-01-14 00:42:39
  • 用PyInstaller把Python代码打包成单个独立的exe可执行文件

    2022-12-02 09:15:11
  • AJAX缓存问题的两种解决方法(IE)

    2008-05-02 20:57:00
  • 基于laravel缓冲cache的用法详解

    2024-05-11 10:07:55
  • python脚本替换指定行实现步骤

    2022-03-18 16:53:28
  • JavaScript实现DOM对象选择器

    2024-05-05 09:15:35
  • python通过定义一个类实例作为ftp回调方法

    2021-08-16 14:12:03
  • turtle的基础使用之python turtle递归绘图

    2023-04-25 23:36:08
  • python面向对象编程设计原则之单一职责原则详解

    2022-02-13 20:51:25
  • [JS]用 或 || 来兼容FireFox

    2013-06-26 14:50:47
  • asp如何让页面过时并指定一个过时时间?

    2010-05-13 16:40:00
  • sqlserver 导出插入脚本代码

    2024-01-26 22:39:18
  • 浅谈JavaScript的自动垃圾收集机制

    2023-08-13 15:18:57
  • python图形用户接口实例详解

    2023-10-17 21:05:20
  • Python中列表(list)操作方法汇总

    2023-11-12 06:16:33
  • Go语言中调用外部命令的方法总结

    2024-05-13 10:44:09
  • 百度编辑器二次开发常用手记整理小结

    2023-10-22 20:28:33
  • asp之家 网络编程 m.aspxhome.com