From 1ada7d14f769744efdfd19c207a83ff3a850f177 Mon Sep 17 00:00:00 2001 From: Richard de Wit Date: Tue, 14 Jan 2020 16:27:23 +0100 Subject: [PATCH 1/5] Add ASGI support Then in your project's `asgi.py` file use this: ```python from configurations.asgi import get_asgi_application application = get_asgi_application() ``` --- configurations/asgi.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 configurations/asgi.py diff --git a/configurations/asgi.py b/configurations/asgi.py new file mode 100644 index 0000000..b257d60 --- /dev/null +++ b/configurations/asgi.py @@ -0,0 +1,14 @@ +from . import importer + +importer.install() + +try: + from django.core.asgi import get_asgi_application +except ImportError: # pragma: no cover + from django.core.handlers.asgi import ASGIHandler + + def get_asgi_application(): # noqa + return ASGIHandler() + +# this is just for the crazy ones +application = get_asgi_application() From c1cb3874f24378d99d1f460eed7e73ab82be325c Mon Sep 17 00:00:00 2001 From: Finn-Thorben Sell Date: Thu, 9 Sep 2021 15:01:11 +0200 Subject: [PATCH 2/5] remove fallback onto ASGIHandler ASGIHandler is considered a django internal api with get_asgi_application being the public counterpart. get_asgi_application is also present since the introduction of django's asgi support so the ImportError should never occur anyways. --- configurations/asgi.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/configurations/asgi.py b/configurations/asgi.py index b257d60..7a28d29 100644 --- a/configurations/asgi.py +++ b/configurations/asgi.py @@ -2,13 +2,7 @@ from . import importer importer.install() -try: - from django.core.asgi import get_asgi_application -except ImportError: # pragma: no cover - from django.core.handlers.asgi import ASGIHandler - - def get_asgi_application(): # noqa - return ASGIHandler() +from django.core.asgi import get_asgi_application # this is just for the crazy ones application = get_asgi_application() From 2cae9838b559049efc979fb8b5f03765a1517abf Mon Sep 17 00:00:00 2001 From: Finn-Thorben Sell Date: Thu, 9 Sep 2021 15:08:42 +0200 Subject: [PATCH 3/5] add documentation to README about get_asgi_application --- README.rst | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 536b154..7b9ac9f 100644 --- a/README.rst +++ b/README.rst @@ -120,5 +120,18 @@ The same applies to your **wsgi.py** file, e.g.: Here we don't use the default ``django.core.wsgi.get_wsgi_application`` function but instead ``configurations.wsgi.get_wsgi_application``. +Or if you are not serving your app via WSGI but ASGI instead, you need to modify your **asgi.py** file too.: + +.. code-block:: python + + import os + + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') + os.environ.setdefault('DJANGO_CONFIGURATION', 'DEV') + + from configurations.asgi import get_asgi_application + + application = get_asgi_application() + That's it! You can now use your project with ``manage.py`` and your favorite -WSGI enabled server. +WSGI/ASGI enabled server. From 6a4a620891cf2dfd6733a0cf63566d86d0d62507 Mon Sep 17 00:00:00 2001 From: Finn-Thorben Sell Date: Thu, 9 Sep 2021 15:11:49 +0200 Subject: [PATCH 4/5] add additional documentation references to asgi.py --- README.rst | 2 +- docs/cookbook.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 7b9ac9f..0716754 100644 --- a/README.rst +++ b/README.rst @@ -81,7 +81,7 @@ command line option, e.g. python manage.py runserver --settings=mysite.settings --configuration=Dev To enable Django to use your configuration you now have to modify your -**manage.py** or **wsgi.py** script to use django-configurations's versions +**manage.py**, **wsgi.py** or **asgi.py** script to use django-configurations's versions of the appropriate starter functions, e.g. a typical **manage.py** using django-configurations would look like this: diff --git a/docs/cookbook.rst b/docs/cookbook.rst index bb5f048..c34cae1 100644 --- a/docs/cookbook.rst +++ b/docs/cookbook.rst @@ -182,7 +182,7 @@ probably just add the following to the **beginning** of your settings module: import configurations configurations.setup() -That has the same effect as using the ``manage.py`` or ``wsgi.py`` utilities. +That has the same effect as using the ``manage.py``, ``wsgi.py`` or ``asgi.py`` utilities. This will also call ``django.setup()``. >= 3.1 From 7c9ac5e53f728a8c7cd7a00c471d68de17e40e5f Mon Sep 17 00:00:00 2001 From: Brian Helba Date: Mon, 25 Oct 2021 09:34:42 -0400 Subject: [PATCH 5/5] Suppress import ordering style error --- configurations/asgi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configurations/asgi.py b/configurations/asgi.py index 7a28d29..da9401b 100644 --- a/configurations/asgi.py +++ b/configurations/asgi.py @@ -2,7 +2,7 @@ from . import importer importer.install() -from django.core.asgi import get_asgi_application +from django.core.asgi import get_asgi_application # noqa: E402 # this is just for the crazy ones application = get_asgi_application()