Cannot import ASGI_APPLICATION module while runserver using channels 2

后端 未结 12 1030
孤独总比滥情好
孤独总比滥情好 2021-01-07 19:59

I have followed the channels tutorial but while running these error throw

Version of the packages is channels==2.1.2 Django==2.0.4<

相关标签:
12条回答
  • 2021-01-07 20:42

    I've recently faced to this problem, and quickly solved it. After checking the configurations in settings.py and routing.py, I found the problem is in this line:

    from channels.auth import AuthMiddlewareStack

    the problem is the version compatibility. Then, I upgraded the requirements.txt to the below and it works quiet well.

    channels==2.4.0
    channels-redis==2.4.2
    daphne==2.5.0
    
    0 讨论(0)
  • 2021-01-07 20:49

    You need to put your routing.py file inside mayapp/mayapp/routing.py instead of mayapp/routing.py

    0 讨论(0)
  • 2021-01-07 20:54

    in my case there were unresolved packages in consumer.py check if you have any unresolved packages in your channels .py files

    0 讨论(0)
  • 2021-01-07 20:57

    You should put routing.py inside mysite/mysite/ not mysite/ otherwise you won't be able to use ASGI_APPLICATION = mysite.routing.application in the settings file and you get that error.

    0 讨论(0)
  • 2021-01-07 20:58

    In case anybody comes along this. Remember: ASGI_APPLICATION = "myapp.routing.application" should go at the bottom of settings.py to ensure nothing get snagged in production!

    mysite/myapp/routing.py

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

    myapp/routing.py

    from django.urls import path
    from . import consumers
    
    websocket_urlpatterns = [
        path('chatroompage', consumers.ChatConsumer),
    ]
    
    0 讨论(0)
  • 2021-01-07 20:58

    When importing your app with absolute path in myapp/routing make sure it is imported like:

    import myapp.routing

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

    It may give you warning as error in PyCharm but you will have working Django channels. I reported it as bug to PyCharm. I hope no one else will spend 3-4 hours on it like me)

    0 讨论(0)
提交回复
热议问题