Skip to main content

access django request object whenever you need it

Project description


gadjo.requestprovider gives access to django’s HTTPRequest object whenever is needed, without explicitly passing it down the path of code.


add gadjo’s requestprovider middleware to django’s

MIDDLEWARE_CLASSES=( ‘gadjo.requestprovider.middleware.RequestProvider’, … )

Whenever you need the request object (from your model, form, etc):

from gadjo.requestprovider.signals import get_request

http_request = get_request()

implementation details

besides saving a reference to the current request, the RequestProvider middleware also registers itself as a receiver for whoever signals that it needs the request object.

whenver a get_request() is issued, the middleware is called to process the signal and returns the request object. this works because the signals in django are processed synchronously. get_request() calls send() on the request_accessor SingleHandlerSignal(Signal) object and returns the current request to the caller. A SingleHandlerSignal is a Signal that makes sure it will only register authorized receivers. This is to make sure no one will register a handler (maybe by mistake) for a request_accessor signal since this is supposed to be handled only by our RequestProvider middleware.

class SingleHandlerSignal(Signal):


the Signal’s connect() method is overwritten to register only allowed receivers.




this is not thread-safe. however, if you are running your django apps in a threaded environment I don’t think you really know what you’re doing.

Project details

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page