Skip to main content

Contains utils for storing and retreiving values from threadlocals, and middleware for placing the current Django request in threadlocal storage.

Project description


Install using pip install django-threadlocals

Add threadlocals.middleware.ThreadLocalMiddleware to your MIDDLEWARE_CLASSES setting. Then use it as follows:

Example usage:

from threadlocals.threadlocals import get_current_request

request = get_current_request()

Caveat Emptor

Storing the request in threadlocals goes against several django core developers' strong inclinations against using threadlocals. See this thread on django-users for a more in-depth discussion. We recommend you not do this unless you have a really good reason to do so. We feel like we had a really good reason (the only one we've come across so far): Having the request in threadlocals is the core piece that allowed us to build a true multi-tenant system on top of django where the site is resolved dynamically based on the current host, and objects are filtered based on the current host. With the current version of django, this is nearly impossible to do without the request in threadlocals. This was a very significant and advanced undertaking, but we're happy with the results.


To run tests:

python tester/ test

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-threadlocals, version 0.10
Filename, size File type Python version Upload date Hashes
Filename, size django_threadlocals-0.10-py3-none-any.whl (6.7 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size django-threadlocals-0.10.tar.gz (5.7 kB) File type Source Python version None Upload date Hashes View

Supported by

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