ASGIでデプロイするLink to this heading

WSGI だけでなく、Django は非同期ウェブサーバやアプリケーションのための新しい Python 標準である 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:

application オブジェクトLink to this heading

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.

The startproject command creates a file <project_name>/asgi.py that contains such an application callable.

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.

Settings モジュールを設定するLink to this heading

When the ASGI server loads your application, Django needs to import the settings module — that's where your entire application is defined.

Django は適切な Settings モジュールを見つけるために、 DJANGO_SETTINGS_MODULE 環境変数を使用します。これは設定モジュールへのドット区切りのパスを含んでいなければなりません。開発時と実運用時で別々の値を使用することも可能です。どのように設定を組織するか次第です。

If this variable isn't set, the default asgi.py sets it to mysite.settings, where mysite is the name of your project.

ASGI ミドルウェアの適用Link to this heading

To apply ASGI middleware, or to embed Django in another ASGI application, you can wrap Django's application object in the asgi.py file. For example:

Code
from some_asgi_library import AmazingMiddleware

application = AmazingMiddleware(application)