BrokenLinkEmailsMiddleware
This class provides middleware functionality to notify site managers via email when a 404 NOT FOUND response is triggered by a broken link. It distinguishes between internal and external referrers and includes logic to ignore specific requests based on project settings or common bot behaviors. The middleware only operates when the application is not in debug mode.
Attributes
| Attribute | Type | Description |
|---|---|---|
| using | string = null | Set to override the mail.mailers alias used for sending the email. |
Methods
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)
Send broken link emails for relevant 404 NOT FOUND responses.
Parameters
| Name | Type | Description |
|---|---|---|
| request | [HttpRequest](../../http/request/httprequest.md?sid=django_http_request_httprequest) | The current request object containing metadata like host, path, and headers. |
| response | [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse) | The response object generated by the view, checked for a 404 status code. |
Returns
| Type | Description |
|---|---|
[HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse) | The original response object, potentially after triggering an email notification if a 404 error occurred. |
send_mail()
@classmethod
def send_mail(
subject: string,
message: string,
*args: any,
**kwargs: any
)
Dispatches an email notification to site managers regarding a broken link, optionally using a specific mailer alias.
Parameters
| Name | Type | Description |
|---|---|---|
| subject | string | The subject line of the email, typically indicating if the link was internal or external. |
| message | string | The body of the email containing details about the referrer, requested URL, user agent, and IP address. |
| *args | any | Additional positional arguments passed to the underlying mail manager function. |
| **kwargs | any | Additional keyword arguments passed to the underlying mail manager function. |
is_internal_request()
@classmethod
def is_internal_request(
domain: string,
referer: string
) - > boolean
Return True if the referring URL is the same domain as the current request.
Parameters
| Name | Type | Description |
|---|---|---|
| domain | string | The hostname of the current request to compare against the referer. |
| referer | string | The HTTP_REFERER header value from the request. |
Returns
| Type | Description |
|---|---|
boolean | True if the referer matches the current domain, False otherwise; subdomains are treated as distinct domains. |
is_ignorable_request()
@classmethod
def is_ignorable_request(
request: [HttpRequest](../../http/request/httprequest.md?sid=django_http_request_httprequest),
uri: string,
domain: string,
referer: string
) - > boolean
Return True if the given request shouldn't notify the site managers according to project settings or in situations outlined by the inline comments.
Parameters
| Name | Type | Description |
|---|---|---|
| request | [HttpRequest](../../http/request/httprequest.md?sid=django_http_request_httprequest) | The current request object used to check project-specific settings. |
| uri | string | The full path of the requested URL being validated. |
| domain | string | The hostname of the current request. |
| referer | string | The referring URL that led to the 404 error. |
Returns
| Type | Description |
|---|---|
boolean | True if the 404 error should be ignored based on empty referers, search engine patterns, or configured URL exclusions. |