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<
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
You need to put your routing.py
file inside mayapp/mayapp/routing.py
instead of mayapp/routing.py
in my case there were unresolved packages in consumer.py check if you have any unresolved packages in your channels .py files
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.
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),
]
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)