ASGI_APPLICATION 不适用于 Django Channels

The*_*ter 18 python django django-channels

我按照频道文档中的教程进行操作,但是当我启动服务器时,python3 manage.py runserver它给了我这个:

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
October 17, 2022 - 00:13:21
Django version 4.1.2, using settings 'config.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Run Code Online (Sandbox Code Playgroud)

当我期望它给我这个时:

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
October 17, 2022 - 00:13:21
Django version 4.1.2, using settings 'config.settings'
Starting ASGI/Channels version 3.0.5 development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Run Code Online (Sandbox Code Playgroud)

设置.py

INSTALLED_APPS = [
    'channels',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    ...
]

ASGI_APPLICATION = 'config.asgi.application'
Run Code Online (Sandbox Code Playgroud)

阿斯吉

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

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')

application = ProtocolTypeRouter({
    'http': get_asgi_application(),
})
Run Code Online (Sandbox Code Playgroud)

ASGI_APPLICATION = 'config.asgi.application即使我将其更改为,它也不会给出任何错误ASGI_APPLICATION = ''

Pat*_*nda 28

这可能是由于您使用的 Django 和频道版本不兼容尝试:channels==3.0.4 and django==4.0.0

  • 谢谢,使用 Python 3.9。 (2认同)

rez*_*afi 12

一开始:

pip install daphne
pip install channels
Run Code Online (Sandbox Code Playgroud)

然后更新setting.py:

INSTALLED_APPS = [
    'daphne',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
Run Code Online (Sandbox Code Playgroud)

只是这个。


小智 11

与 django=4.1.4 相同,channels=4.0.0

我的解决方案:

  1. 使用 daphne 安装频道(我没有删除或对以前安装的频道执行任何操作;在同一个 venv 中操作)

python -m pip install -U channels["daphne"]

  1. 将 daphne 放入已安装的应用程序中,不要将频道放在那里:

INSTALLED_APPS = ( "daphne", "django.contrib.auth",...

  1. 在settings.py中添加ASGI_APPLICATION:

ASGI_APPLICATION = "myproject.asgi.application"