How to deploy with ASGI
Django’s startproject management command sets up a default ASGI configuration for you, which you can tweak as needed for your project, and direct any ASGI-compliant application server to use.
Django includes getting-started documentation for the following ASGI servers:
Like WSGI, ASGI has you supply an application
callable which the application server uses to communicate with your code. It’s commonly provided as an object named application
in a Python module accessible to the server.
It’s not used by the development server (runserver
), but can be used by any ASGI server either in development or in production.
ASGI servers usually take the path to the application callable as a string; for most Django projects, this will look like myproject.asgi:application
.
Warning
Do not call blocking synchronous functions or libraries in any async code. Django prevents you from doing this with the parts of Django that are not async-safe, but the same may not be true of third-party apps or Python libraries.
When the ASGI server loads your application, Django needs to import the settings module — that’s where your entire application is defined.
Django uses the DJANGO_SETTINGS_MODULE environment variable to locate the appropriate settings module. It must contain the dotted path to the settings module. You can use a different value for development and production; it all depends on how you organize your settings.
To apply ASGI middleware, or to embed Django in another ASGI application, you can wrap Django’s application
object in the file. For example: