Complete the code to define an async middleware class in Django.
class MyAsyncMiddleware: async def __call__(self, request): response = await self.[1](request) return response
The async middleware class calls the stored get_response coroutine with the request to get the response asynchronously.
Complete the async middleware __init__ method to store the next callable.
class MyAsyncMiddleware: def __init__(self, [1]): self.get_response = get_response
The __init__ method receives the next callable in the chain, named get_response, which it stores for later use.
Fix the error in the async middleware call method to await the response.
class MyAsyncMiddleware: async def __call__(self, request): response = [1] self.get_response(request) return response
Since get_response is an async callable, it must be awaited to get the response.
Fill both blanks to add async pre- and post-processing in middleware.
class MyAsyncMiddleware: async def __call__(self, request): await [1](request) response = await self.get_response(request) await [2](response) return response
Async middleware can define async methods for pre-processing the request and post-processing the response, called before and after the main call.
Fill all three blanks to create an async middleware that logs request and response.
class LoggingAsyncMiddleware: def __init__(self, [1]): self.get_response = get_response async def __call__(self, request): print(f"Request path: {request.[2]") response = await self.get_response(request) print(f"Response status: {response.[3]") return response
The middleware stores the next callable in get_response, then logs the request's path and the response's status_code.