Man*_*hit 9 python django django-middleware chain-of-responsibility request-response
这样,图就很清楚了。
但我有一些问题
当 process_request() 中间件出现异常时会发生什么?如何处理?会调用 response_middleware 吗?例如。如果异常进来process_view()
的AuthenticationMiddleware
,然后会process_response()
的MessageMiddleware
调用?
当 process_response() 中间件返回响应时会发生什么?例如。如果process_view()
的AuthenticationMiddleware
回报阶跃响应,然后会process_response()
的MessageMiddleware
调用?或者它会从折回AuthenticationMiddleware
(即,它会调用process_response()
的AuthenticationMiddleware
,只是不会调用process_response()
的MessageMiddleware
)
我已经调试了 1.10 中使用新样式中间件类的 django 的行为,但我对旧MIDDLEWARE_CLASSES
设置不熟悉?
对于 django 1.10:- 1) 如果process_request()
forAuthenticationMiddleware
返回响应,则process_template_response()
和process_response()
将被调用,如下图所示,用于所有中间件。
2) 如果process_request()
forAuthenticationMiddleware
引发异常,那么行为也将相同。
纠正我,如果我错了。
提前致谢。
对于2),你是对的。函数convert_exception_to_response()
将捕获引发的异常process_request()
。
查看来源:
https://github.com/django/django/blob/master/django/core/handlers/base.py
https://github.com/django/django/blob/master/django/core/handlers /异常.py