You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django Channels路由错误:'/lobby/'路径未找到,请求技术排查

Troubleshooting Django Channels "Not Found "/lobby/"" Routing Error

Let’s break down the possible causes for this error and walk through fixes step by step:

1. Check if you’re mixing Channels 1.x and 2.x+ syntax

Looking at your routing.py, you’re using route() and channel_routing—that’s the old syntax for Channels 1.x. If you’ve installed Channels 2.0 or later (the current stable version), this routing setup won’t work at all.

For Channels 2+, you need to rewrite your routing to use ProtocolTypeRouter, URLRouter, and class-based consumers. Here’s how to fix it:

Updated routing.py (Channels 2.x+):

from django.urls import re_path
from . import game_consumers

# Use websocket_urlpatterns instead of channel_routing
websocket_urlpatterns = [
    re_path(r"^lobby/$", game_consumers.LobbyConsumer.as_asgi()),
]

Updated consumer example (class-based):

from channels.generic.websocket import WebsocketConsumer

class LobbyConsumer(WebsocketConsumer):
    def connect(self):
        # Your ws_connect_lobby logic here
        self.accept()

    def receive(self, text_data):
        # Your ws_receive_lobby logic here
        pass

    def disconnect(self, close_code):
        # Your ws_disconnect_lobby logic here
        pass

Also update your asgi.py to wire up the routing:

from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
import your_app_name.routing  # Replace with your actual app name

application = ProtocolTypeRouter({
    "http": get_asgi_application(),
    "websocket": AuthMiddlewareStack(
        URLRouter(
            your_app_name.routing.websocket_urlpatterns
        )
    ),
})

2. Verify path matching accuracy

Your current regex r"^/lobby/$" expects an exact match for /lobby/ (with leading and trailing slashes). If your frontend is connecting to /lobby (no trailing slash) or has any extra characters, it won’t match.

  • Fix 1: Adjust your frontend to connect to the exact path:
    // Make sure the URL ends with a slash to match your regex
    Websocket.lobby_socket = new ReconnectingWebSocket(`ws://${window.location.host}/lobby/`);
    
  • Fix 2: Loosen the regex to allow an optional trailing slash:
    # Channels 1.x:
    route('websocket.connect', game_consumers.ws_connect_lobby, path=r"^/lobby/?$"),
    # Channels 2.x+:
    re_path(r"^lobby/?$", game_consumers.LobbyConsumer.as_asgi()),
    
    Note: In Channels 2.x+, the leading slash in the regex is usually omitted because Django's URL routing handles the base path.

3. Confirm your Channels setup is properly configured

  • Make sure channels is added to your INSTALLED_APPS in settings.py.
  • Check that you’ve set up a channel layer (even using the in-memory layer for development works):
    # settings.py
    CHANNEL_LAYERS = {
        "default": {
            "BACKEND": "channels.layers.InMemoryChannelLayer"
        }
    }
    
  • If using a production server like Daphne, ensure it’s pointing to your asgi.py instead of the regular wsgi.py.

4. Test if the consumer is being triggered

Add a print statement or logger to your consumer’s connect method to see if it’s ever being hit. For example:

# Channels 1.x consumer
def ws_connect_lobby(message):
    print("Lobby websocket connection attempted!")
    # Rest of your logic

If you don’t see this message in your server logs when trying to connect, the routing isn’t matching at all—go back and double-check the version and path regex.


内容的提问来源于stack exchange,提问作者Michael

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:34:16