尝试通过Django通道运行Django应用时出现语法错误

时间:2019-09-16 21:10:19

标签: python django django-channels

我正在按照https://realpython.com/getting-started-with-django-channels/的教程使用Django频道。

它正在使用django==1.10.5 channels==1.0.2 asgi_redis==1.0.0

零件很少

main_app / routing.py:

from channels.routing import route
from example.consumers import ws_connect, ws_disconnect


channel_routing = [
    route('websocket.connect', ws_connect),
    route('websocket.disconnect', ws_disconnect),
]

example / views.py:

def user_list(request):
    return render(request, 'example/user_list.html')

example / consumers.py:

from channels import Group

def ws_connect(message):
    Group('users').add(message.reply_channel)

def ws_disconnect(message):
    Group('users').discard(message.reply_channel)

应用程序很小,但是当我尝试运行服务器时,出现以下错误:

Unhandled exception in thread started by <function wrapper at 0x0000000004566518>
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\django\utils\autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django\core\management\commands\runserver.py", line 116, in inner_run
    autoreload.raise_last_exception()
  File "C:\Python27\lib\site-packages\django\utils\autoreload.py", line 251, in raise_last_exception
    six.reraise(*_exception)
  File "C:\Python27\lib\site-packages\django\utils\autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django\__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Python27\lib\site-packages\django\apps\registry.py", line 116, in populate
    app_config.ready()
  File "C:\Python27\lib\site-packages\channels\apps.py", line 22, in ready
    monkeypatch_django()
  File "C:\Python27\lib\site-packages\channels\hacks.py", line 10, in monkeypatch_django
    from .management.commands.runserver import Command as RunserverCommand
  File "C:\Python27\lib\site-packages\channels\management\commands\runserver.py", line 5, in <module>
    from daphne.server import Server, build_endpoint_description_strings
  File "C:\Python27\lib\site-packages\daphne\server.py", line 213
    async def handle_reply(self, protocol, message):

当我访问主页localhost:8000时,它说“连接被拒绝”。如何删除此错误并使应用运行?

0 个答案:

没有答案