【问题标题】:cannot import name 'Channel' from 'channels'无法从“频道”导入名称“频道”
【发布时间】:2022-01-17 04:54:46
【问题描述】:

我正在使用 python version==3.8.10 和 Django==2.2 和 channels==3.0.4 。我收到错误

从频道导入频道 ImportError:无法从“频道”(/home/kritik/py38_djanngo2.2_new/lib/python3.8/site-packages/channels/init.py)导入名称“频道”

这就是我的代码的样子。这是我使用 channels=1.1.2 和 python 2.7 和 Django=1.11.13 时的旧代码。现在当我升级时出现错误我需要更新代码吗?如果是,那我需要更新什么?

from channels import Channel
class TestWebSocket(APIView):

    def post(self, format=None):
        try:
            REPLY_CHANNEL = self.request.data['request_channel']
            # job = Job(
            #     name= new_data[0]['change_type'],
            #     status="COS initiation has started. Please be patient.",
            # )
            # job.save()
            # Tell client task has been started

            Channel(REPLY_CHANNEL).send({
                "text": json.dumps({
                    "action": "started",
                })
            })
            return Response({"Success": "Sucess"}, status=status.HTTP_200_OK)
        except Exception as e:
            return Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST)

谁能帮我解决这个问题?

【问题讨论】:

    标签: python-3.x django django-rest-framework django-channels


    【解决方案1】:

    我在单元测试中遇到了同样的错误。

    # Chat/tests.py
    
    from channels import Channel
    from channels.test import ChannelTestCase
    
    class MyTests(ChannelTestCase):
        def test_a_thing(self):
            # This goes onto an in-memory channel, not the real backend.
            Channel("some-channel-name").send({"foo": "bar"})
    

    我正在学习 Django 文档中的本教程: https://channels.readthedocs.io/en/1.x/testing.html

    由于某种原因,无法识别频道包。我使用以下命令在我的虚拟环境中安装了频道:

    python -m pip install -U channels
    

    【讨论】:

      猜你喜欢
      • 2019-12-23
      • 1970-01-01
      • 2014-10-13
      • 1970-01-01
      • 2022-07-16
      • 2012-11-14
      • 1970-01-01
      • 2017-05-21
      • 1970-01-01
      相关资源
      最近更新 更多