gpt4 book ai didi

django-channels - 我如何在 Django-Channels 中嵌套 URLRouter

转载 作者:行者123 更新时间:2023-12-02 04:24:18 24 4
gpt4 key购买 nike

我曾尝试使用 django.urls.pathdjango.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: : include() is not supported in URLRouter. Use nested URLRouter instances instead.



我在文档中找到了评论,但没有示例:
https://channels.readthedocs.io/en/latest/releases/2.1.0.html?highlight=URLRouter#nested-url-routing

最佳答案

而不是使用 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,
),
})

关于django-channels - 我如何在 Django-Channels 中嵌套 URLRouter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56239070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com