Module not found error with Django Channels

46 views Asked by At

I'm completely new to using channels, but have tried two separate installations, and setups to just get a basic version of channels running and am encountering a small issue. I'm assuming I'm probably just missing a small thing, but I cannot figure it out.

File structure:

mysite:
   mysite:
       __init__.py
       asgi.py
       setting.py
       urls.py
       wsgi.py
   testproject:
       __init__.py
       consumers.py
       routing.py
   manage.py

The error is at handshake

WebSocket HANDSHAKING /ws/livec/ [127.0.0.1:51020]
Exception inside application: 'module' object is not callable
Traceback (most recent call last):
File "C:  \Users\LTV2g\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\django\contrib\staticfiles\handlers.py", line 101, in __call__
return await self.application(scope, receive, send)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'module' object is not callable
WebSocket DISCONNECT /ws/livec/ [127.0.0.1:51020]

I'll also show the current file structure, as well as the files that I've actually made modifications to.

asgi.py

import os

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
from ..testproject import routing

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

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

settings.py

INSTALLED_APPS = [
    'daphne',
    'channels',
    'testproject',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

...
ASGI_APPLICATION = "mysite.settings"
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels.layers.InMemoryChannelLayer"
    },
}

consumers.py

import json
from channels.generic.websocket import WebsocketConsumer


class Calculator(WebsocketConsumer):
    def connect(self):
        self.accept()

    def disconnect(self, close_code):
        self.close()

    def receive(self, text_data):
        pass
        #text_data_json = json.loads(text_data)
        #expression = text_data_json['expression']

routing.py

from django.urls import re_path

from . import consumers

websocket_urlpatterns = [
    re_path(r'ws/livec/$', consumers.Calculator.as_asgi()),
]

Originally, in settings.py having ASGI_APPLICATION set as mysite.asgi.application was resulting in an error so I changed that to mysite.settings and it seemed to work correctly when starting. However, on handshake now this issue occurs, so I feel like that could potentially contribute to it. I've tried on different projects and both encounter this issue.

0

There are 0 answers