我正在尝试使用 asgi 而不是 wsgi 来运行我的 django 项目。我已按如下方式设置了我的 routing.py 和 asgi.py:
路由.py
from django.conf.urls import url
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator,
OriginValidator
from channels.auth import AuthMiddlewareStack
from some.consumers import SomeConsumer
application = ProtocolTypeRouter({
'websocket': AllowedHostsOriginValidator(
AuthMiddlewareStack(
URLRouter(
[
url(r'^some_url/$', SomeConsumer),
]
)
)
)
})
asgi.py
import os
from channels.routing import get_default_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
application = get_default_application()
现在,正如您所看到的,它是标准设置,并且可以在默认 django 服务器上正常工作,但是当我尝试与其他一些(daphne 或 uvicorn)一起运行时,它会抛出此异常。
这是我的INSTALLED_APPS
INSTALLED_APPS = [
'channels',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django_jenkins',
'easy_thumbnails',
'image_cropping',
'allauth',
'allauth.account',
'and_rest_of_my_own_apps',
]
有没有人遇到过这样的问题?
- - 编辑 - -
这是一个堆栈跟踪:
Traceback (most recent call last):
File "/path/to/my/env/bin/daphne", line 11, in <module>
sys.exit(CommandLineInterface.entrypoint())
File "/path/to/my/env/lib/python3.6/site-packages/daphne/cli.py", line 161, in entrypoint
cls().run(sys.argv[1:])
File "/path/to/my/env/lib/python3.6/site-packages/daphne/cli.py", line 222, in run
application = import_by_path(args.application)
File "/path/to/my/env/lib/python3.6/site-packages/daphne/utils.py", line 12, in import_by_path
target = importlib.import_module(module_path)
File "/path/to/my/env/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "./my_project/asgi.py", line 5, in <module>
application = get_default_application()
File "/path/to/my/env/lib/python3.6/site-packages/channels/routing.py", line 33, in get_default_application
module = importlib.import_module(path)
File "/path/to/my/env/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "./my_project/routing.py", line 4, in <module>
from channels.auth import AuthMiddlewareStack
File "/path/to/my/env/lib/python3.6/site-packages/channels/auth.py", line 12, in <module>
from django.contrib.auth.models import AnonymousUser
File "/path/to/my/env/lib/python3.6/site-packages/django/contrib/auth/models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/path/to/my/env/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "/path/to/my/env/lib/python3.6/site-packages/django/db/models/base.py", line 100, in __new__
app_config = apps.get_containing_app_config(module)
File "/path/to/my/env/lib/python3.6/site-packages/django/apps/registry.py", line 244, in get_containing_app_config
self.check_apps_ready()
File "/path/to/my/env/lib/python3.6/site-packages/django/apps/registry.py", line 127, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
和命令
daphne -b 0.0.0.0 -p 8000 my_project.asgi:application --access-log=./logs/daphne-access.log