LoginSignup
1
0

More than 1 year has passed since last update.

Djangoで詰まったエラーまとめ

Last updated at Posted at 2021-07-11

1, websocketの導入

myapp 
   └── myapp 
         ├── myapp 
         |     ├── urls.py
         |     └── settings.py
         |
         ├── chat  
         |     ├── urls.py
         |     └── consumers.py

こんな感じのサーバを作ってたとき(結構省略している)
中身の詳細→https://poyo.hatenablog.jp/entry/2018/05/17/224247
websocketを取り入れたサーバ作りたいな〜と思い

%pip3 install channels
を実行し、
"INSTALLED_APPS"に"channels"を追加すると

CommandError: You have not set ASGI_APPLICATION, which is needed to run the server.

"ASGI_APPLICATION"が設定されていないというエラーが出るので、例のサイトを参考にして
myapp/settings.pyに入れてみる。

ASGI_APPLICATION = 'myapp.routing.application'

しかし、またエラー

django.core.exceptions.ImproperlyConfigured: Cannot import ASGI_APPLICATION module 'myapp.routing'

ここでroutingをasgiにすると

ASGI_APPLICATION = 'myapp.asgi.application'

うまくいく

Django version 3.2.4, using settings 'myapp.settings'
Starting ASGI/Channels version 3.0.3 development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

理由は分からない。

2, redis-serverを立ち上げる

せっかく動いたのにまたエラー

ConnectionRefusedError: [Errno 61] Connect call failed ('127.0.0.1', 6379)
WebSocket DISCONNECT /ws/chat/aa/ [127.0.0.1:51813]

調べたところによるとredis-serverが動いていないことが原因らしい
%pip3 install channels_redis
%brew install redis-server
%redis-server
^Z
これでデーモンで動いてくれる

さらにsettings.pyにも色々書き込む

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            "hosts": [('127.0.0.1', 6379)],
        },
    },
}

これで動きました(結果が載せられなくてすいません)
redis-serverは基本的に6379というポート番号を使うんですかね
psコマンドで確認できます

また、redis-serverを止めたいときは
%kill -9 redis-server

3, 実はまだ解決できていないエラー

channel layerを有効化しようと例のサイトを参考にして
chatのconsumers.pyのChatConsumerというクラスを更新していたところ

await self.channel_layer.group_add(
      self.room_group_name, self.channel_name
)

何度やってもここのコードでwebsocket disconnectとなってしまう
同期、非同期も試したが意味なし
self.channel_nameの中身に原因がありそうで
print(self.channel_name)を書き足して実行してみるが

specific.2cdf4c3db1e14e28b30e1baac0f78305!f6f0c02fca0c46c0a5e4d

なんかよく分からない文字列

誰か助けてください

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0