如何解决简单的 django 频道应用程序未与 daphne 一起运行
我试图让 django 频道与 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",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
频道==3.0.3
我从他们的网站 https://channels.readthedocs.io/en/stable/tutorial/part_1.html
成功完成了频道设置但是当我尝试一个简单的时,它最终不适用于达芙妮:
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
)
),})
在 runserver 之前我也尝试过:
export DJANGO_SETTINGS_MODULE=mysite.settings
在做频道教程时,他们要求我们确保频道层可以与 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()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。