作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试让 django channel 与 daphne 一起运行,但我总是以这个结束
x8/backback/mysite » daphne mysite.asgi:application 1 ↵
Traceback (most recent call last):
File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/bin/daphne", line 8, in <module>
sys.exit(CommandLineInterface.entrypoint())
File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/daphne/cli.py", line 170, in entrypoint
cls().run(sys.argv[1:])
File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/daphne/cli.py", line 232, in run
application = import_by_path(args.application)
File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/daphne/utils.py", line 12, in import_by_path
target = importlib.import_module(module_path)
File "/Library/Frameworks/Python.framework/Versions/3.8/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 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "./mysite/asgi.py", line 11, in <module>
from channels.auth import AuthMiddlewareStack
File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/channels/auth.py", line 12, in <module>
from django.contrib.auth.models import AnonymousUser
File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/django/contrib/auth/models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/django/contrib/auth/base_user.py", line 48, in <module>
class AbstractBaseUser(models.Model):
File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/django/db/models/base.py", line 108, in __new__
app_config = apps.get_containing_app_config(module)
File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
self.check_apps_ready()
File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/django/apps/registry.py", line 135, in check_apps_ready
settings.INSTALLED_APPS
File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/django/conf/__init__.py", line 82, in __getattr__
self._setup(name)
File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/django/conf/__init__.py", line 63, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
我有
Django==3.1.7
达芙妮==3.0.1
channel ==3.0.3
我成功地从他们的网站完成了 channel 设置 https://channels.readthedocs.io/en/stable/tutorial/part_1.html
但最终当我尝试一个简单的方法时,它不适用于 daphne:
daphne mysite.asgi:application
或
daphne -p 8001 mysite.asgi:application
这是我当前的asgi.py
import os
import django
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
import chat.routing
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
django.setup()
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(
URLRouter(
chat.routing.websocket_urlpatterns
)
),
})
在运行服务器之前我也尝试过:
export DJANGO_SETTINGS_MODULE=mysite.settings
在制作 channel 教程时,他们要求我们确保 channel 层可以与 Redis 通信(教程的第 2 部分)
$ python3 manage.py shell
>>> import channels.layers
>>> channel_layer = channels.layers.get_channel_layer()
>>> from asgiref.sync import async_to_sync
>>> async_to_sync(channel_layer.send)('test_channel', {'type': 'hello'})
>>> async_to_sync(channel_layer.receive)('test_channel')
{'type': 'hello'}
如果我直接使用 python3 shell 而不是 python3 manage.py shell,我会收到与尝试运行 daphne 时遇到的类似错误。我想也许有联系,但我不知道在哪里以及为什么。我花了好几天的时间,一遍又一遍地做,请帮忙~
最佳答案
是的,伙计们,在你的 asgi.py 中,您必须将以下代码移至您的之前“从channels.auth导入AuthMiddlewareStack”:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '****.settings')
django.setup()
看图片 /image/szSw4.jpg
关于django - 简单的 django channel 应用程序未与 daphne 一起运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66379627/
我是一名优秀的程序员,十分优秀!