Django 频道问题。请帮助。
路由.py
websocket_urlpatterns = [
url(r'ws/chat/room/(?P<room_id>\d+)/$', ChatConsumer.as_asgi()),]
room.html
var chatSocket = new WebSocket(
'ws://' + window.location.host +
'/ws/chat/room/{{ room.id }}/'
);
项目名称/urls.py
path('chat/', include('chat.urls', namespace='chat')),
聊天/urls.py
path('room/<int:room_id>/', views.room, name='room'),
path('room/<int:room_id>/send_message/', views.send_message, name='send_message'),
websocket.url =
ws://127.0.0.1:8000/ws/chat/room/2/
Exception inside application: No route found for path 'ws/chat/room/2'.
Traceback (most recent call last):
File "C:\Users\snapk\PycharmProjects\site\venv\lib\site-packages\django\contrib\staticfiles\handlers.py", line 101, in __call__
return await self.application(scope, receive, send)
File "C:\Users\snapk\PycharmProjects\site\venv\lib\site-packages\channels\routing.py", line 62, in __call__
return await application(scope, receive, send)
File "C:\Users\snapk\PycharmProjects\site\venv\lib\site-packages\channels\sessions.py", line 47, in __call__
return await self.inner(dict(scope, cookies=cookies), receive, send)
File "C:\Users\snapk\PycharmProjects\site\venv\lib\site-packages\channels\sessions.py", line 263, in __call__
return await self.inner(wrapper.scope, receive, wrapper.send)
File "C:\Users\snapk\PycharmProjects\site\venv\lib\site-packages\channels\auth.py", line 185, in __call__
return await super().__call__(scope, receive, send)
File "C:\Users\snapk\PycharmProjects\site\venv\lib\site-packages\channels\middleware.py", line 24, in __call__
return await self.inner(scope, receive, send)
File "C:\Users\snapk\PycharmProjects\site\venv\lib\site-packages\channels\routing.py", line 134, in __call__
raise ValueError("No route found for path %r." % path)
ValueError: No route found for path 'ws/chat/room/2'.
多次更改 re_path,要求 chatgpt 编写新的 html 模板(我更喜欢 Django,然后是 JS)。