Django daphne:应用程序尚未加载

问题描述 投票:0回答:2

当我尝试跑步时

daphne -p 8001 messanger.asgi:application
我收到此错误:django.core.exceptions.AppRegistryNotReady:尚未加载应用程序。 如果我在没有达芙妮的情况下运行服务器,我会收到此错误:收听失败:无法收听 127.0.0.1:8000:[Errno 48] 地址已在使用中。

这是我的设置.py:

INSTALLED_APPS = [
    "channels",
    "chat",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    'django_extensions',
]

ASGI_APPLICATION = "messanger.asgi.application"

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [os.environ.get("REDIS_URL", "redis_url")],
        },
    },
}

这是我的 asgi.py:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "messanger.settings")
django_asgi_app = get_asgi_application()
django.setup()


application = ProtocolTypeRouter({
    # Django's ASGI application to handle traditional HTTP requests
    "http": django_asgi_app,

    # WebSocket chat handler
    "websocket": AllowedHostsOriginValidator(
        AuthMiddlewareStack(
            URLRouter(chat.routing.websocket_urlpatterns)
        )
    ),
})

这是我的消费者.py:

class ChatConsumer(WebsocketConsumer):
    def connect(self):
        self.room_name = self.scope["url_route"]["kwargs"]["room_name"]
        self.room_group_name = "chat_%s" % self.room_name

        # Join room group
        channel_layer = get_channel_layer()
        async_to_sync(channel_layer.group_add)(
            self.room_group_name, self.channel_name
        )

        self.accept()

    def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json["message"]
        author_id = text_data_json["a"]
        chat_id = text_data_json["chat"]

        message_create = Message(content=message, author_id=author_id, chat_id=chat_id)
        message_create.save()

        print("message:", message, "author_id:", author_id, "chat_id:", chat_id)

        channel_layer = get_channel_layer()
        async_to_sync(channel_layer.group_send)(
            self.room_group_name,
            {
                "type": "chat_message",
                "message": message,
                "author_id": author_id,
                "chat_id": chat_id
            }
        )

    def chat_message(self, event):
        message = event["message"]
        author_id = event["author_id"]
        author_username = User.objects.get(id=author_id).username
        chat_id = event["chat_id"]

        self.send(text_data=json.dumps({
            "type": "chat",
            "message": message,
            "chat_id": chat_id,
            "author_id": author_id,
            "author_username": author_username
        }))

    def disconnect(self, close_code):
        channel_layer = get_channel_layer()
        async_to_sync(channel_layer.group_discard)(
            self.room_group_name,
            self.channel_name
        )
python django django-channels daphne
2个回答
0
投票

首先,我建议在 asgi.py 中将 get_asgi_application() 直接移动到 Router 中:

application = ProtocolTypeRouter(
    {
        "http": get_asgi_application(),
        ...
    }

您是否同时运行 WSGI 和 Daphne 服务器? Websocket 功能不适用于 WSGI,因此 Daphne 旨在替代您的 Web 服务器。澄清一下,Django 频道功能不适用于 WSGI。

这个问题肯定是因为另一个服务器在另一个终端的后台运行,可能是 Django 在 localhost/127.0.0.1:8000 或 Daphne 上的开发服务器。

你说你尝试在端口 Daphne -p 8001 上运行 daphne 所以 Daphne 服务器应该在 127.0.0.1:8001 上启动,也许你输入了 8000?


0
投票

我必须在 django_asgi_app = get_asgi_application() 下导入移动模型

© www.soinside.com 2019 - 2024. All rights reserved.