Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i nest URLRouter in Django-Channels

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

like image 287
Antonio Romero Oca Avatar asked May 21 '19 13:05

Antonio Romero Oca


People also ask

What is ProtocolTypeRouter?

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 .

What are Django channels?

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.

What is ASGI py file in Django?

ASGI (Asynchronous Server Gateway Interface) provides an interface between async Python web servers and applications while it supports all the features provided by WSGI.

What is routing in Django?

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.

How to include a router url in Django?

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...

How do I use simplerouter with Django?

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.

What is channels in Django?

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,

Does urlrouter nesting work with path () routes?

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.


1 Answers

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,
    ),
})
like image 81
rapi Avatar answered Oct 25 '22 20:10

rapi