django中process_request发生错误时中间件的执行顺序是怎样的?

Man*_*hit 9 python django django-middleware chain-of-responsibility request-response

我正在研究 django 中间件代码库。我查看了下图在此处输入图片说明

这样,图就很清楚了。

但我有一些问题

  1. 当 process_request() 中间件出现异常时会发生什么?如何处理?会调用 response_middleware 吗?例如。如果异常进来process_view()AuthenticationMiddleware,然后会process_response()MessageMiddleware调用?

  2. 当 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引发异常,那么行为也将相同。

纠正我,如果我错了。

提前致谢。

wya*_*byn 0

对于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