Skip to main content

Detetermine the context under which a Django project is run (provides a get_run_context function)

Project description

django-run-context

Provides a simple function that returns the run context that Django finds itself in.

The following values are currently supported:

  • "runserver_reloader"
  • "runserver_noreloader"
  • "runserver_reloaded"
  • "not runserver"

To use this just add to your settings.py:*

from django_run_context import get_run_context

RUN_CONTEXT = get_run_context()

and then anywhere else in you Django project you can access it:

```python
import django.conf.settings

print(settings.RUN_CONTEXT)
```

and use it to alter the behaviour of your app based on that context.

The immediate need it addressed was a desire to dump project diagnostics in debug mode, in the settings.py. For example a common paradigm I employ:

if DEBUG:
    log.debug(f"Django version: {django.__version__}")
    log.debug(f"Python version: {sys.version}")
    log.debug(f"Django loaded from: {django.__file__}")
    log.debug(f"Using Path: {sys.path}")
    log.debug(f"Process Info: {pinfo()}")
    log.debug(f"Static root: {STATIC_ROOT}")
    log.debug(f"Static file dirs: {locals().get('STATICFILES_DIRS', globals().get('STATICFILES_DIRS', []))}")
    log.debug(f"Installed apps: {INSTALLED_APPS}")
    log.debug(f"Database: {DATABASES['default']}")
    log.debug(f"Command line: {sys.argv}")

But this is spewed out twice, because by default runserver loads settings.py then starts a new instance of runserver that provides a web interface (at http://127.0.0.1:8000 by default) which again loads settings.py (the supervising instance of runserver does not act as a webserver at all, but watches the filesystem for changes and reruns the actual webserver instance). So, we can suppress the output under the first instance (the reloader)

if DEBUG and RUN_CONTEXT != "runserver_reloader":
    log.debug(f"Django version: {django.__version__}")
    log.debug(f"Python version: {sys.version}")
    log.debug(f"Django loaded from: {django.__file__}")
    log.debug(f"Using Path: {sys.path}")
    log.debug(f"Process Info: {pinfo()}")
    log.debug(f"Static root: {STATIC_ROOT}")
    log.debug(f"Static file dirs: {locals().get('STATICFILES_DIRS', globals().get('STATICFILES_DIRS', []))}")
    log.debug(f"Installed apps: {INSTALLED_APPS}")
    log.debug(f"Database: {DATABASES['default']}")
    log.debug(f"Command line: {sys.argv}")

and it is spewed put just once when the debug project starts up whether using reloader (the default) or not.

This can of course easily be extended for tests that are more specific than "not runserver", possibly telling us if uwsgi, gunicorn or daphne or some other context. Feel free to expand on it if you like (PRs gladly accepted).

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

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

Source Distribution

django_run_context-1.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_run_context-1.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file django_run_context-1.0.tar.gz.

File metadata

  • Download URL: django_run_context-1.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for django_run_context-1.0.tar.gz
Algorithm Hash digest
SHA256 0667f449da9028d3b7a50b5831547437c048d14b34c96f1b011cdeb34ff4eec3
MD5 f9e43882bf08c264e59e292de73a9499
BLAKE2b-256 4cf1c873eb2ed54c526c5cf08b7f0cdf8ba3266aff017dcab7e3e02ca0ed9029

See more details on using hashes here.

File details

Details for the file django_run_context-1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_run_context-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cd677dda4428a9a3ef9f4bcc012f015274af9abaac2d071a86488cac6f2c5896
MD5 790bd38fbb947a60fb7b16ca422deefa
BLAKE2b-256 8808ffc131eda1522e7fdd855b0f04e21cc75ecbba39523fe0df4dc075163d75

See more details on using hashes here.

Supported by

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