class documentation

class CommonMiddleware(MiddlewareMixin):

View In Hierarchy

"Common" middleware for taking care of some basic operations:

  • Forbid access to User-Agents in settings.DISALLOWED_USER_AGENTS

  • URL rewriting: Based on the APPEND_SLASH and PREPEND_WWW settings, append missing slashes and/or prepends missing "www."s.

    • If APPEND_SLASH is set and the initial URL doesn't end with a slash, and it is not found in urlpatterns, form a new URL by appending a slash at the end. If this new URL is found in urlpatterns, return an HTTP redirect to this new URL; otherwise process the initial URL as usual.

    This behavior can be customized by subclassing CommonMiddleware and overriding the response_redirect_class attribute.

Method get​_full​_path​_with​_slash Return the full path of the request with a trailing slash appended.
Method process​_request Check for denied User-Agents and rewrite the URL based on settings.APPEND_SLASH and settings.PREPEND_WWW
Method process​_response When the status code of the response is 404, it may redirect to a path with an appended slash if should_redirect_with_slash() returns True.
Method should​_redirect​_with​_slash Return True if settings.APPEND_SLASH is True and appending a slash to the request path turns an invalid path into a valid one.

Inherited from MiddlewareMixin:

Async Method __acall__ Async version of __call__ that is swapped in when an async request is running.
Method __call__ Undocumented
Method __init__ Undocumented
Method __repr__ Undocumented
Method ​_async​_check If get_response is a coroutine function, turns us into async mode so a thread is not consumed during a whole request.
Class Variable async​_capable Undocumented
Class Variable sync​_capable Undocumented
Instance Variable ​_is​_coroutine Undocumented
Instance Variable get​_response Undocumented
def get_full_path_with_slash(self, request):

Return the full path of the request with a trailing slash appended.

Raise a RuntimeError if settings.DEBUG is True and request.method is POST, PUT, or PATCH.

def process_request(self, request):
Check for denied User-Agents and rewrite the URL based on settings.APPEND_SLASH and settings.PREPEND_WWW
def process_response(self, request, response):
When the status code of the response is 404, it may redirect to a path with an appended slash if should_redirect_with_slash() returns True.
def should_redirect_with_slash(self, request):
Return True if settings.APPEND_SLASH is True and appending a slash to the request path turns an invalid path into a valid one.