Django ·达芙妮:应用程序尚未加载

w41d8nur  于 2023-05-01  发布在  Go
关注(0)|答案(2)|浏览(138)

当我尝试运行daphne -p 8001 messanger.asgi:application时,我得到以下错误:django.core.exceptions.AppRegistryNotReady:应用程序尚未加载。如果我在没有daphne的情况下运行服务器,我会得到这个错误:侦听失败:第127章不能听0.0.1:8000:[错误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")],
        },
    },
}

这是我的 www.example.com

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)
        )
    ),
})

这是我的 www.example.com

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
        )
6mw9ycah

6mw9ycah1#

首先,我推荐 www.example.com 你直接在Router内部移动get_asgi_application():

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

你是否同时运行WSGI和Daphne服务器?WebSocket功能将无法与WSGI一起工作,因此Daphne将成为您的Web服务器替代品。需要澄清的是,Django通道功能将不适用于WSGI。
这个问题很可能是因为另一个服务器在另一个终端的后台运行,可能是Django在localhost/127上的开发服务器。0.0.1:8000或达芙妮。
您说您尝试在端口Daphne -p 8001上运行Daphne,因此Daphne服务器应该在127上启动。0.0.1:8001,也许你输入了8000?

pvcm50d1

pvcm50d12#

我所要做的就是在django_asgi_app = get_asgi_application()下移动模型导入

相关问题