Middleware¶
middleware
¶
LoginRequiredMiddleware(get_response)
¶
Middleware that redirects unauthenticated users to the login page.
Checks every request and redirects to settings.LOGIN_URL if the user
is not authenticated, unless the path matches one of the configured open
URLs.
Settings
LOGIN_URL: The URL path to redirect unauthenticated users to.
OPEN_URLS: Dict of regex patterns for paths that do not require
authentication (e.g. {'signup': '^signup/$'}). The login URL
is always included automatically.
The next query parameter is appended to the redirect URL so the user
returns to the originally requested page after login.
CRequestMiddleware
¶
Bases: MiddlewareMixin
Thread-local storage for the current HTTP request.
Stores the active request per-thread so that code outside views (e.g. model methods, signals, utility functions) can access it without explicit parameter passing.
Usage
process_request(request)
¶
Store the current request.
process_response(request, response)
¶
Delete the current request to avoid leaking memory.
get_request(default=None)
classmethod
¶
Retrieve the request object for the current thread, or the optionally provided default if there is no current request.
set_request(request)
classmethod
¶
Save the given request into storage for the current thread.
del_request()
classmethod
¶
Delete the request that was stored for the current thread.
ExceptionTrackerMiddleware(get_response)
¶
Bases: ErrorTracker
Middleware that captures unhandled exceptions into the Error model.
Place at the end of MIDDLEWARE so it catches exceptions from all
other middleware and views. Each unique exception is deduplicated by hash;
repeated occurrences increment the count.
For capturing exceptions in try/except blocks, use the module-level
error_tracker instance directly instead.