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

yipeiwu_com5年前Python基础

实例如下所示:

#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,上一层会处理报错)。

以上这篇从django的中间件直接返回请求的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python使用MD5加密字符串示例

Python加密模块有好几个,但无论是哪种加密方式都需要先导入相应的加密模块然后再使用模块对字符串加密。 先导入md5加密所需模块: 复制代码 代码如下: import hashlib...

Python中实现switch功能实例解析

前言 今天在学习python的过程中,发现python没有switch这个语法。于是就想在python中如何才能实现这个功能呢? 正文 本文中我们对switch的使用模拟为正常的数据库的...

Python3.6安装及引入Requests库的实现方法

Python3.6安装及引入Requests库的实现方法

本博客可能没有那么规范,环境之类的配置。只是让你直接开始编程写python。 至于各种配置网络上有多种方法。 本文仅代表我的观点的一种方法。 电脑环境:win10 64位 第一步:下载p...

Django实现一对多表模型的跨表查询方法

当有两个表,例如一个学生表,一个班级表,是多对一的关系。 方法1: c = models.Class.object.get(pk=1) #查询到ID为1的班级 stus = mode...

python opencv实现运动检测

本文实例为大家分享了python opencv运动检测的具体代码,供大家参考,具体内容如下 # -*- coding:utf-8 -*- __author__ = 'kingking...