我是第一次尝试Django Channels。我正在学习这个教程-https://www.youtube.com/watch?v=cw8-KFVXpTE&t=21s-它基本上解释了Channels的基础知识。我使用pip install channels
在我的虚拟环境中安装了Channels,它安装了最新版本,也就是4.0.0。我已经布置了基本的设置。但是当我运行python manage.py runserver
时,我得到-
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
January 10, 2023 - 02:37:40
Django version 4.1.3, using settings 'BrokerBackEnd.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
当我应该得到这个-
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
January 10, 2023 - 02:37:40
Django version 4.1.3, using settings 'BrokerBackEnd.settings'
Starting ASGI/Channels version 4.0.0 development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
我不知道我在这方面做错了甚么,也找不到任何有关的解决方法,我非常感谢任何人的建议。
- 设置. py**
INSTALLED_APPS = [
'channels',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'rest_framework',
'Orders',
]
ASGI_APPLICATION = "BrokerBackEnd.asgi.application"
- 指定名称. py**
import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
import Orders.routing as route
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'BrokerBackEnd.settings')
application = ProtocolTypeRouter({
'http': get_asgi_application(),
'websocket': URLRouter(
route.websocket_urlpatterns
)
})
- 消费者. py**
class OrderConsumer(WebsocketConsumer):
def connect(self):
self.accept()
def receive(self, text_data=None, bytes_data=None):
self.send(text_data="Message = " + text_data)
def disconnect(self, code):
return super().disconnect(code)
- 路由. py**
websocket_urlpatterns = [
re_path(r"socket/order/", OrderConsumer.as_asgi())
]
1条答案
按热度按时间owfi6suc1#
从django-channels 4.0.0开始,daphne服务器与其他频道分离。现在,如果你想使用daphne,你需要将它包含在你的settings.pyINSTALLED_APPS中。将它包含在你安装的应用的顶部。你正在查看的教程使用的是旧版本的频道。
此外,请确保实际安装了daphne: