使用django中间件时出现ContentNotRenderedError [英] ContentNotRenderedError while using django middleware

查看:875
本文介绍了使用django中间件时出现ContentNotRenderedError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在django中间件中使用了process_request和process_response函数,以便在访问视图集之前记录请求。但是,我收到内部服务器错误。我不明白我的代码有什么问题。

I used process_request and process_response functions in django middleware in order to log the requests before hitting the viewset. But, I get Internal Server Error. I don't understand what's wrong with my code.

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 )

我缺少什么?
错误是

What am I missing? The error is

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'
----------------------------------------


推荐答案

问题是返回中间件方法中的Response()

如果您在 process_request 方法,那么Django将不会调用该视图。您可能不想在这里返回任何内容。

If you return a response in the process_request method, then Django will not call the view. You probably don't want to return anything here.

您应该返回 process_response 方法。您可能要在此处从视图返回原始响应(响应)。如果您返回其他响应(例如 Response(...)),则Django会将其返回给用户,而不是视图中的响应。

You should return a response from the process_response method. You probably want to return the original response from the view (response) here. If you return a different response (e.g. Response(...)), then Django will return this to the user instead of the response from the view.

class MyMiddleware(): 
    def process_request(self, request): 
        print "xxxxx"

    def process_response(self, request, response): 
        print "xxxxx"
        return response

有关应从每个中间件方法返回的内容的更多信息,请参见Django文档。

See the Django docs for more information about what you should return from each middleware method.

这篇关于使用django中间件时出现ContentNotRenderedError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆