Skip to main content

Django global context

Project description

django-g

Django global context to get current request from anywhere in your application.

Many scenarios need the current request but Django don't have it accessible. They expect that your application implement a middleware to use the request, but it is burdensome and not reusable.

Other packages, like django-threadlocals do the same thing with threadlocals, but it doesn't work on the async world.

How to install

$ pip install django-g

How to use

Add django_g.middleware.RequestMiddleware to your settings MIDDLEWARE. It is a small middleware just to capture the current request and save to global context, ordering probably doesn't matter because any other middleware already have access to the request.

MIDDLEWARE = [
    "django_g.middleware.RequestMiddleware",
    ...
]
from django_g import get_current_request


def your_func():
    request = get_current_request()
    # Use the request here. Be careful and handle when `request=None`.

protip It is not a good idea to get the request everywhere, because you're coupling the framework specifics with your logic, so use this package only to get the request where you don't have a better way.

Project details


Download files

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

Source Distribution

django-g-0.0.2.tar.gz (3.4 kB view hashes)

Uploaded Source

Built Distribution

django_g-0.0.2-py3-none-any.whl (4.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page