我在django中间件中使用了process_request和process_response函数,以便在命中视图集之前记录请求。但是,我得到内部服务器错误。我不明白我的代码有什么问题。
class MyMiddleware():
def process_request( self, request ):
print "xxxxx"
return Response( status=status.HTTP_200_OK )
def process_response( self, request, response ):
print "xxxxx"
return Response( status=status.HTTP_200_OK )
我错过了什么? 错误是
Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 86, in run
self.finish_response()
File "/usr/lib/python2.7/wsgiref/handlers.py", line 127, in finish_response
for data in self.result:
File "/usr/local/lib/python2.7/dist-packages/django/template/response.py", line 118, in __iter__
raise ContentNotRenderedError('The response content must be '
ContentNotRenderedError: The response content must be rendered before it can be iterated over.
----------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 593, in process_request_thread
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/local/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 126, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "/usr/lib/python2.7/SocketServer.py", line 649, in __init__
self.handle()
File "/usr/lib/python2.7/wsgiref/simple_server.py", line 124, in handle
handler.run(self.server.get_app())
File "/usr/lib/python2.7/wsgiref/handlers.py", line 92, in run
self.close()
File "/usr/lib/python2.7/wsgiref/simple_server.py", line 33, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
----------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 86, in run
self.finish_response()
File "/usr/lib/python2.7/wsgiref/handlers.py", line 127, in finish_response
for data in self.result:
File "/usr/local/lib/python2.7/dist-packages/django/template/response.py", line 118, in __iter__
raise ContentNotRenderedError('The response content must be '
ContentNotRenderedError: The response content must be rendered before it can be iterated over.
----------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 593, in process_request_thread
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/local/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 126, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "/usr/lib/python2.7/SocketServer.py", line 649, in __init__
self.handle()
File "/usr/lib/python2.7/wsgiref/simple_server.py", line 124, in handle
handler.run(self.server.get_app())
File "/usr/lib/python2.7/wsgiref/handlers.py", line 92, in run
self.close()
File "/usr/lib/python2.7/wsgiref/simple_server.py", line 33, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
----------------------------------------
答案 0 :(得分:2)
问题在于中间件方法中的return Response()
。
如果您在process_request
方法中返回响应,则Django将不会调用该视图。你可能不想在这里退货。
您应该从process_response
方法返回回复。您可能希望在此处返回视图(response
)中的原始响应。如果您返回不同的响应(例如Response(...)
),则Django会将此返回给用户而不是视图中的响应。
class MyMiddleware():
def process_request(self, request):
print "xxxxx"
def process_response(self, request, response):
print "xxxxx"
return response
有关从每个中间件方法返回的内容的更多信息,请参阅Django文档。
答案 1 :(得分:1)
我解决了这个问题。这是因为process_response返回的响应对象没有实际响应。我把它修改为跟随并且有效。
class MyMiddleware():
def process_request( self, request ):
print "xxxxx"
return None
def process_response( self, request, response ):
print "xxxxx"
return response