我尝试使用django.urls.path
和django.urls.include
来构造 django-channels 路由中的URL。
例如:
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,
),
),
})
但是我得到了:
django.core.exceptions.ImproperlyConfigured::URLRouter不支持include()。采用 嵌套的URLRouter实例。
我在文档中找到了注释,但没有示例: https://channels.readthedocs.io/en/latest/releases/2.1.0.html?highlight=URLRouter#nested-url-routing
答案 0 :(得分:0)
我没有使用include
,而是通过嵌套URLRouter
来解决它。
假设browser_routing.urls
也是一组嵌套的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,
),
})