Home  >  Article  >  Backend Development  >  Directly return the requested method from Django's middleware

Directly return the requested method from Django's middleware

不言
不言Original
2018-06-01 14:35:362043browse

This article mainly introduces the method of returning requests directly from Django's middleware. It has certain reference value. Now I share it with you. Friends in need can refer to it

The example is 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":"记录事件开始到数据库出错"}),
   content_type='application/json'
   )
 return None

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

Then when returning, you can jsondump the dictionary content you want to return (if not dumped, the upper layer will handle the error).

Related recommendations:

Detailed explanation of calling static files for Django learning

The above is the detailed content of Directly return the requested method from Django's middleware. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn