Skip to main content

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

AttributeTypeDescription
usingstring = nullSet 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

NameTypeDescription
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

TypeDescription
[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

NameTypeDescription
subjectstringThe subject line of the email, typically indicating if the link was internal or external.
messagestringThe body of the email containing details about the referrer, requested URL, user agent, and IP address.
*argsanyAdditional positional arguments passed to the underlying mail manager function.
**kwargsanyAdditional 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

NameTypeDescription
domainstringThe hostname of the current request to compare against the referer.
refererstringThe HTTP_REFERER header value from the request.

Returns

TypeDescription
booleanTrue 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

NameTypeDescription
request[HttpRequest](../../http/request/httprequest.md?sid=django_http_request_httprequest)The current request object used to check project-specific settings.
uristringThe full path of the requested URL being validated.
domainstringThe hostname of the current request.
refererstringThe referring URL that led to the 404 error.

Returns

TypeDescription
booleanTrue if the 404 error should be ignored based on empty referers, search engine patterns, or configured URL exclusions.