Skip to main content

CommonMiddleware

"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.

Attributes

AttributeTypeDescription
response_redirect_classclass = HttpResponsePermanentRedirectThe HTTP redirect class used when performing URL rewriting for missing slashes or 'www' prefixes.

Constructor

Signature

def CommonMiddleware(
get_response: callable = None
) - > None

Parameters

NameTypeDescription
get_responsecallable = NoneA function or method that takes a request and returns a response, representing the next middleware or view in the chain.

Methods


process_request()

@classmethod
def process_request(
request: [HttpRequest](../../http/request/httprequest.md?sid=django_http_request_httprequest)
) - > HttpResponseRedirect | None

Check for denied User-Agents and rewrite the URL based on settings.APPEND_SLASH and settings.PREPEND_WWW

Parameters

NameTypeDescription
request[HttpRequest](../../http/request/httprequest.md?sid=django_http_request_httprequest)The incoming HTTP request object containing metadata and path information.

Returns

TypeDescription
`HttpResponseRedirectNone`

should_redirect_with_slash()

@classmethod
def should_redirect_with_slash(
request: [HttpRequest](../../http/request/httprequest.md?sid=django_http_request_httprequest)
) - > boolean

Return True if settings.APPEND_SLASH is True and appending a slash to the request path turns an invalid path into a valid one.

Parameters

NameTypeDescription
request[HttpRequest](../../http/request/httprequest.md?sid=django_http_request_httprequest)The current HTTP request used to validate the path against the URL configuration.

Returns

TypeDescription
booleanTrue if the current path is invalid but would be valid with a trailing slash, and the view allows appending.

get_full_path_with_slash()

@classmethod
def get_full_path_with_slash(
request: [HttpRequest](../../http/request/httprequest.md?sid=django_http_request_httprequest)
) - > string

Return the full path of the request with a trailing slash appended. Raise a RuntimeError if settings.DEBUG is True and request.method is DELETE, POST, PUT, or PATCH.

Parameters

NameTypeDescription
request[HttpRequest](../../http/request/httprequest.md?sid=django_http_request_httprequest)The HTTP request object from which the full path and method are extracted.

Returns

TypeDescription
stringThe complete URL path including query strings with a slash safely appended to the path component.

process_response()

@classmethod
def process_response(
request: [HttpRequest](../../http/request/httprequest.md?sid=django_http_request_httprequest),
response: [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)
) - > [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)

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.

Parameters

NameTypeDescription
request[HttpRequest](../../http/request/httprequest.md?sid=django_http_request_httprequest)The HTTP request object associated with the response.
response[HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)The response object generated by the view or previous middleware.

Returns

TypeDescription
[HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)The original response, a redirect response if a slash was appended, or the response updated with a Content-Length header.