CORS Middleware

class smarter.lib.django.middleware.cors.SmarterCorsMiddleware(get_response)[source]

Bases: CorsMiddleware, SmarterHelperMixin

CORS middleware that applies django-cors-headers only while ENABLE_MIDDLEWARE_CORS is on.

Design rules:

  • no shared request state on self

  • no coroutine guessing in core logic

  • one class for sync and async mode

The allowed origins come from the CORS_* Django settings, through corsheaders.middleware.CorsMiddleware. In sync mode, an amnesty path skips the middleware too.

Example

To enable this middleware, add it to your Django project’s middleware settings:

MIDDLEWARE = [
    ...
    'smarter.lib.django.middleware.cors.SmarterCorsMiddleware',
    ...
]
__init__(get_response)[source]

Initialize django-cors-headers, and detect whether the next handler is async.

Parameters:

get_response – The next handler in the middleware chain.

property formatted_class_name: str[source]

Return the formatted class name for logging purposes.

It names the instance, not a request.