從django的中間件直接返回請求的方法
更新時間:2018年05月30日 15:14:59 作者:Iphone4
今天小編就為大家分享一篇從django的中間件直接返回請求的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
實例如下所示:
#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":"記錄事件開始到數(shù)據(jù)庫出錯"}),
content_type='application/json'
)
return None
如上代碼所示,需要從django的http模塊導入HttpResponse類,
然后返回的時候可以把自己想要返回的字典內(nèi)容用jsondump一把(如果不dump,上一層會處理報錯)。
以上這篇從django的中間件直接返回請求的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python利用 SVM 算法實現(xiàn)識別手寫數(shù)字
支持向量機 (Support Vector Machine, SVM) 是一種監(jiān)督學習技術(shù),它通過根據(jù)指定的類對訓練數(shù)據(jù)進行最佳分離,從而在高維空間中構(gòu)建一個或一組超平面。本文將介紹通過SVM算法實現(xiàn)手寫數(shù)字的識別,需要的可以了解一下2021-12-12

