跳到主要内容

channels

修改asgi.py

asgi.py
"""
ASGI config for unionUser project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

from channels.routing import ProtocolTypeRouter
from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "union.settings")

# Initialize Django ASGI application early to ensure the AppRegistry
# is populated before importing code that may import ORM models.
django_asgi_app = get_asgi_application()

application = ProtocolTypeRouter({
"http": django_asgi_app,
# Just HTTP for now. (We can add other protocols later.)
})

修改settings.py

settings.py
INSTALLED_APPS = [
'rest_framework', # 增加这一行
'corsheaders', # 这里增加允许跨域请求头配置
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
'channels',
]

ASGI_APPLICATION = 'myproject.wsgi.application'

WSGI_APPLICATION = "union.wsgi.application"

安装Daphne

可以通过pip安装daphne

pip install daphne

用Daphne启动Django

备注

此时不应该使用python manage.py runserver启动项目了!

当Daphne安装完成后,对应的终端命令便可执行以用来开启Daphne进程。

备注

最简单的使用,daphne命令需要传参一个ASGI应用,并且跟随application,冒号分隔。

比如对于一个最典型的Django项目,可以输入如下命令:

daphne myproject.asgi:application

这将会在127.0.0.1:8000开启一个进程。注意该命令执行位置应该和你的manage.py文件同级目录下。