Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

When using Django Channels, in my routing.py, this works:

from django.conf.urls import url
from channels.http import AsgiHandler
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import my_app.routing

application = ProtocolTypeRouter({
    'http': AuthMiddlewareStack(
        URLRouter (
            my_app.routing.http_urlpatterns +
            [ url("", AsgiHandler) ]
        )
    )
})

But this doesn't:

from django.urls import path               # <--- Changed
from channels.http import AsgiHandler
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import my_app.routing

application = ProtocolTypeRouter({
    'http': AuthMiddlewareStack(
        URLRouter (
            my_app.routing.http_urlpatterns +
            [ path("", AsgiHandler) ] # <--- Changed
        )
    )
})

When you run Django using path, it runs, but if you try to load any pages, you get an exception like:

2021-02-05 09:42:10,420 ERROR    Traceback (most recent call last):
  File "/opt/my_proj/venv3/lib64/python3.6/site-packages/daphne/http_protocol.py", line     "server": self.server_addr,
ValueError: No route found for path 'auth/login/'.

I'm using Django version 2.2.10 and channels version 2.3.0.

I was under the impression that url was the old way, and path was that new way, and even though their usage is slightly different, path should be able to do anything url can. Am I using it wrong? Or is this one place url MUST be used?

In the documentation, even though all the examples seem to use url, it does say:

[URLRouter] Takes a single argument, a list of Django URL objects (either path() or url())

question from:https://stackoverflow.com/questions/66066148/why-does-django-channels-asgihandler-not-work-with-path

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
437 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...