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
| Attribute | Type | Description |
|---|---|---|
| response_redirect_class | class = HttpResponsePermanentRedirect | The HTTP redirect class used when performing URL rewriting for missing slashes or 'www' prefixes. |
Constructor
Signature
def CommonMiddleware(
get_response: callable = None
) - > None
Parameters
| Name | Type | Description |
|---|---|---|
| get_response | callable = None | A 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
| Name | Type | Description |
|---|---|---|
| request | [HttpRequest](../../http/request/httprequest.md?sid=django_http_request_httprequest) | The incoming HTTP request object containing metadata and path information. |
Returns
| Type | Description |
|---|---|
| `HttpResponseRedirect | None` |
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
| Name | Type | Description |
|---|---|---|
| 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
| Type | Description |
|---|---|
boolean | True 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
| Name | Type | Description |
|---|---|---|
| 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
| Type | Description |
|---|---|
string | The 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
| Name | Type | Description |
|---|---|---|
| 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
| Type | Description |
|---|---|
[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. |