I have tried to use django.urls.path
and django.urls.include
to structure the URLs in the routing for django-channels.
Ex:
from django.urls import path, include
from .browser import routing as browser_routing
websocket_urlpatterns = [
path('ws/v2/', include([
path('browser/', browser_routing.urls),
])),
]
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from my_channels import routing
application = ProtocolTypeRouter({
'websocket': AuthMiddlewareStack(
URLRouter(
routing.websocket_urlpatterns,
),
),
})
But i got:
django.core.exceptions.ImproperlyConfigured: : include() is not supported in URLRouter. Use nested URLRouter instances instead.
I found the comment in the documentation but no examples: https://channels.readthedocs.io/en/latest/releases/2.1.0.html?highlight=URLRouter#nested-url-routing
ProtocolTypeRouter. channels.routing.ProtocolTypeRouter. This should be the top level of your ASGI application stack and the main entry in your routing file. It lets you dispatch to one of a number of other ASGI applications based on the type value present in the scope .
Channels is a project that takes Django and extends its abilities beyond HTTP - to handle WebSockets, chat protocols, IoT protocols, and more. It's built on a Python specification called ASGI. Channels builds upon the native ASGI support available in Django since v3.
ASGI (Asynchronous Server Gateway Interface) provides an interface between async Python web servers and applications while it supports all the features provided by WSGI.
A route can be defined as a URL that displays a particular web page on the browser. For example, if we want to visit the Educative website, we would head over to https://www.educative.io. Django lets us route URLs however we want and with no framework limitations.
The .urls attribute on a router instance is simply a standard list of URL patterns. There are a number of different styles for how you can include these URLs. For example, you can append router.urls to a list of existing views... Alternatively you can use Django's include function, like so...
REST framework adds support for automatic URL routing to Django, and provides you with a simple, quick and consistent way of wiring your view logic to a set of URLs. Here's an example of a simple URL conf, that uses SimpleRouter. There are two mandatory arguments to the register () method: prefix - The URL prefix to use for this set of routes.
Channels is a project that takes Django and extends its abilities beyond HTTP - to handle WebSockets, chat protocols, IoT protocols, and more. It’s built on a Python specification called ASGI. Channels builds upon the native ASGI support available in Django since v3.0,
Please note that URLRouter nesting will not work properly with path () routes if inner routers are wrapped by additional middleware. See Issue #1428. Routes channel type scopes based on the value of the channel key in their scope.
Instead of using include
, I was able to fix it by nesting URLRouter
.
assuming that browser_routing.urls
is also a set of nested URLRouter
.
urls.py
from channels.routing import URLRouter
websocket_urlpatterns = URLRouter([
path('ws/v2/', URLRouter([
path('browser/', browser_routing.urls),
])),
])
routing.py
application = ProtocolTypeRouter({
'websocket': AuthMiddlewareStack(
routing.websocket_urlpatterns,
),
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With