Skip to content

Base

AsyncMiddlewareMixin

a base class to create async only middlewares.

it is very similar to django's MiddlewareMixin with the following specification:

__call__ method is async, so you need to await the middleware instance.

>>> middleware = AsyncMiddlewareMixin(get_response)
>>> await middleware()

where get_response is an async function, sync functions are not supported and will raise an error.

Note: you can use middlewares drove from this base class with normal django middlewares, you can even write sync views get_response is usually provided by django, so you don't have to worry about it being async.


other methods are as follows:

  • process_request and process_response are awaited inside the middleware and have to be async.

  • process_view and process_template_response can be either sync or async, but async is preferred, if it's sync django will wrap it as async which might have slight performance reduction.

  • process_exception can be either sync or async, but sync is preferred, if async is used django wraps the method to be called synchronously.