Django channels实现websocket时,报错django.core.exceptions.ImproperlyConfigured: Cannot import ASGI_APPLIC

Django项目使用channels实现websocket时,

项目文件:mysite/asgi.py

import os
import django
from django.core.asgi import get_asgi_application

from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack  
from socketServer import routing

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
django.setup()

application = ProtocolTypeRouter({  
    "http": get_asgi_application(),  
    "websocket": URLRouter(routing.websocket_urlpatters), 
})

项目文件:mysite/setting.py

ASGI_APPLICATION = 'mysite.asgi.application'

启动报错:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/jianyijianse/work/wf/python/djangoApp/djServer/canda/myenv/lib/python3.9/threading.py", line 980, in _bootstrap_inner
    self.run()
  File "/Users/jianyijianse/work/wf/python/djangoApp/djServer/canda/myenv/lib/python3.9/threading.py", line 917, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/jianyijianse/.local/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/Users/jianyijianse/.local/lib/python3.9/site-packages/channels/management/commands/runserver.py", line 101, in inner_run
    application=self.get_application(options),
  File "/Users/jianyijianse/.local/lib/python3.9/site-packages/channels/management/commands/runserver.py", line 126, in get_application
    return StaticFilesWrapper(get_default_application())
  File "/Users/jianyijianse/.local/lib/python3.9/site-packages/channels/routing.py", line 31, in get_default_application
    raise ImproperlyConfigured("Cannot import ASGI_APPLICATION module %r" % path)
django.core.exceptions.ImproperlyConfigured: Cannot import ASGI_APPLICATION module 'mysite.asgi'```


**原因:**
channels版本问题,原版本Django4.2, channels2.0.4



解决:
改为:channels4.1.0
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured是一个常见的误。这个误提示表明在访问设置之前,需要配置Django的设置。 解决这个问题的方法有两种: 1. 定义环境变量DJANGO_SETTINGS_MODULE: 在项目的根目录中,可以通过在终端中使用export命令来定义环境变量DJANGO_SETTINGS_MODULE,例如: export DJANGO_SETTINGS_MODULE=<项目名称>.settings 然后再次运行项目,应该就不会再了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [django调试问题django.core.exceptions.ImproperlyConfigured](https://blog.csdn.net/weixin_44602651/article/details/120456631)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Djangodjango.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS解决办法](https://blog.csdn.net/jyr2014/article/details/126753776)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝with黑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值