websocket 需要使用django rest框架启动AGSI服务器

ttcibm8c  于 2023-10-20  发布在  Go
关注(0)|答案(1)|浏览(163)

我是WebSocket的初学者,我需要与Django频道聊天。当我运行命令启动ASGI服务器时,我得到了这个错误
daphne -B 0.0.0.0-p 8001 project.asgi:application raise ImproperlyConfigured(django.core.exceptions.ImproperlyConfigured:请求设置INSTALLED_APPS,但未配置设置。在访问设置之前,必须定义环境变量DJANGO_SETTINGS_MODULE或调用settings.configure()。
这里是asgi.py

import os
import django
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from chat.routing import websocket_urlpatterns
from django.conf import settings

settings.configure(project.settings) 

application = ProtocolTypeRouter({
    "http": get_asgi_application(),
    "websocket": URLRouter(
        websocket_urlpatterns
    ),
})

settings.py:

INSTALLED_APPS = [
    'daphne',
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles", 
    
    'djoser',
    'rest_framework',
    'rest_framework_simplejwt',
    'channels',
    'channels_redis',
    
    
    # apps
    "accounts",
    "chat"
]
ASGI_APPLICATION = "project.asgi.application"
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("127.0.0.1", 6379)],
        },
    },
}

我需要启动Web Socket服务器为聊天应用程序发出API请求

s2j5cfk0

s2j5cfk01#

在asgi.py的开头添加以下行

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')

相关问题