Skip to main content
 首页 » 编程设计

python之Django channel 'No application configured for scope type ' websocket''

2025年05月04日25kuangbin

我正在尝试根据本教程 (http://channels.readthedocs.io/en/latest/tutorial/part_2.html) 实现与 Django 和 channel 的聊天。我向已安装的应用程序添加 channel 和聊天应用程序。我为一个项目制定了以下路线:

# mysite/routing.py 
from channels.routing import ProtocolTypeRouter 
 
application = ProtocolTypeRouter({ 
    # (http->django views is added by default) 
}) 

基本上,我完全按照教程中的步骤进行操作。但是在 runserver 之后,我仍然得到 ValueError: No application configured for scope type 'websocket',在进入一个特定的聊天室之后。有人可以帮助我吗?

请您参考如下方法:

您似乎缺少 websocket 键。 tutorial说要添加以下导入并在 mysite/routing.py 中添加 websocket 键。

from channels.auth import AuthMiddlewareStack 
from channels.routing import ProtocolTypeRouter, URLRouter 
import chat.routing 
 
application = ProtocolTypeRouter({ 
    # (http->django views is added by default) 
    'websocket': AuthMiddlewareStack( 
        URLRouter( 
            chat.routing.websocket_urlpatterns 
        ) 
    ), 
})