websocket Djangochannelsrestframework不像文档中那样工作

ippsafx7  于 2023-05-22  发布在  Go
关注(0)|答案(2)|浏览(163)

我正在尝试使用djangochannersrestframework创建WebSocket连接。我尝试了所有类似的文档,我得到了这个错误“django.core.exceptions.AppRegistryNotReady:应用程序尚未加载。”
https://djangochannelsrestframework.readthedocs.io/
我的Traceback

Traceback (most recent call last):
  File "/home/user/Projects/igroteka/venv/bin/daphne", line 8, in <module>
    sys.exit(CommandLineInterface.entrypoint())
  File "/home/user/Projects/igroteka/venv/lib/python3.8/site-packages/daphne/cli.py", line 170, in entrypoint
    cls().run(sys.argv[1:])
  File "/home/user/Projects/igroteka/venv/lib/python3.8/site-packages/daphne/cli.py", line 232, in run
    application = import_by_path(args.application)
  File "/home/user/Projects/igroteka/venv/lib/python3.8/site-packages/daphne/utils.py", line 12, in import_by_path
    target = importlib.import_module(module_path)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/user/Projects/igroteka/./igroteka/asgi.py", line 12, in <module>
    import socketapp.routing
  File "/home/user/Projects/igroteka/./socketapp/routing.py", line 2, in <module>
    from socketapp import consumers
  File "/home/user/Projects/igroteka/./socketapp/consumers.py", line 1, in <module>
    from games.models import Games
  File "/home/user/Projects/igroteka/./games/models.py", line 11, in <module>
    class GamesCategories(models.Model):
  File "/home/user/Projects/igroteka/venv/lib/python3.8/site-packages/django/db/models/base.py", line 127, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/user/Projects/igroteka/venv/lib/python3.8/site-packages/django/apps/registry.py", line 260, in get_containing_app_config
    self.check_apps_ready()
  File "/home/user/Projects/igroteka/venv/lib/python3.8/site-packages/django/apps/registry.py", line 138, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Sentry is attempting to send 2 pending error messages
Waiting up to 2 seconds
Press Ctrl-C to quit

这是我的github仓库来检查代码https://github.com/muradoff101/igroteka/tree/master

uxhixvfz

uxhixvfz1#

如果你的消费者导入了你的模型,那么你需要在导入路由之前在asgi.py中调用get_asgi_application(),否则Django在初始化Django之前创建模型时会抛出错误
asgi.py:

import os
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'igroteka.settings')
django_asgi_app = get_asgi_application() # Call this before importing routing

import socketapp.routing

application = ProtocolTypeRouter({
    "http": django_asgi_app,
    "websocket": AuthMiddlewareStack(
        URLRouter(
            socketapp.routing.websocket_urlpatterns
        )
    ),
})
xyhw6mcr

xyhw6mcr2#

也许您安装了最新的默认通道版本4,这不会在开发模式下启动ASGI服务器。请验证这一点并安装渠道版本== 3.0.5 .运行runserver命令时,请验证ASGI服务器是否启动。
(版本4通过daphne支持ASGI服务器,我猜是这样,没有试过。)

相关问题