Method to return the request directly from django's middleware

  • 2020-10-23 20:09:51
  • OfStack

Examples are as follows:


#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":" There was an error recording the event start to the database "}),
   content_type='application/json'
   )
 return None

As shown above, you need to import the HttpResponse class from the http module of django,

You can then return the contents of the dictionary you want to return using jsondump1 (if you do not return dump, the upper level will handle the error).


Related articles: