request will be recorded by the middleware and executed in async view, but in some cases, it will not be executed in view.
Settings:
Python: 3.10.12
Django: 4.2.1
middleware settings:
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"corsheaders.middleware.CorsMiddleware",
"django.middleware.common.CommonMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
...
"utils.middleware.RequestMiddleware",
]
RequestMiddleware:
class RequestMiddleware(MiddlewareMixin):
def process_view(self, request, view_func, view_args, view_kwargs):
req_str = "reqeust user:{} request method:{} request path:{} request body:{}".format(
request.user,
request.META["REQUEST_METHOD"],
request.path,
request.body
)
logger.info(req_str)
return None
def process_response(self, request, response):
data = response.content
data = data.decode("utf-8")
logger.info("response status code:{} data:{}".format(response.status_code, data))
return response
def process_exception(self, request, exception):
logger.error(traceback.format_exc())
views.py:
async def report(request):
my_views.delay() # celery task
return send_message({"result": 0})