如何知道哪些django中间件是异步启用的,哪些没有



要了解Django需要适应什么中间件,可以打开Django的调试日志。请求记录器并查找有关"同步中间件…已调整"的日志消息。

我一直在尝试做同样的事情,但没有任何运气。

这是我的settings.py文件:

LOGGING = {  
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'root': {
'handlers': ['console'],
'level': 'DEBUG',
},
'loggers': {
'django.request': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': True,
},
},
}

尽管我已经设置了LOGGING变量,但我并没有得到文档中提到的输出。

Starting server at tcp:port=8000:interface=127.0.0.1
HTTP/2 support not enabled (install the http2 and tls Twisted extras)
Configuring endpoint tcp:port=8000:interface=127.0.0.1
Listening on TCP address 127.0.0.1:8000
HTTP b'GET' request for ['127.0.0.1', 42684]
HTTP 200 response started for ['127.0.0.1', 42684]
HTTP close for ['127.0.0.1', 42684]
HTTP response complete for ['127.0.0.1', 42684]
127.0.0.1:42684 - - [22/Mar/2022:12:11:47] "GET /admin/" 200 3550
HTTP b'GET' request for ['127.0.0.1', 42684]
HTTP 200 response started for ['127.0.0.1', 42684]
HTTP close for ['127.0.0.1', 42684]
HTTP response complete for ['127.0.0.1', 42684]
127.0.0.1:42684 - - [22/Mar/2022:12:11:48] "GET /admin/core/user/" 200 9028
HTTP b'GET' request for ['127.0.0.1', 42684]
HTTP 200 response started for ['127.0.0.1', 42684]
HTTP close for ['127.0.0.1', 42684]
HTTP response complete for ['127.0.0.1', 42684]
127.0.0.1:42684 - - [22/Mar/2022:12:11:48] "GET /admin/jsi18n/" 200 3343

当我使用运行daphne服务器时

daphne项目名称.asgi:应用程序

命令。有人能帮我得到关于哪些中间件是异步的,哪些不是异步的输出吗。

我试着通过浏览器创建一个视图并请求它,但它似乎没有打印出任何中间件。尽管已经有了可能使用中间件的管理模型,但还没有像";同步中间件。。。"适配";。

我在问题中使用的记录器非常正确,可以查看哪些中间件是否启用了异步。但问题是,我想知道为什么Django提供的中间件没有在这里显示哪些启用了异步,哪些没有。因此,我尝试自己制作一个中间件来检查这个特性。

中间件.py

class SimpleMiddleware(MiddlewareMixin):
def __init__(self, get_response):
self.async_capable = False
self.get_response = get_response
# One-time configuration and initialization.
def __call__(self, request):
# Code to be executed for each request before
# the view (and later middleware) are called.
response = self.get_response(request)
# Code to be executed for each request/response after
# the view is called.
return response

现在,如果您将这个中间件添加到settings.py文件中,并运行服务器,那么您将看到日志将按预期说明这个中间件。

最新更新