最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Getting error message running Django server - Stack Overflow

programmeradmin1浏览0评论

I’m not able to run python manage.py runserver. I was able to run python manage.py migrate successfully. I even changed ASGI_APPLICATION = "MyProject.asgi.application" to ASGI_APPLICATION = "routing.application" which didn’t work. Here is the error I get

  show_sunset_warning()
System check identified no issues (0 silenced).
March 29, 2025 - 17:52:24
Django version 5.1.7, using settings 'MyProject.settings'
Starting ASGI/Daphne version 4.1.2 development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/home/corey-james/Arborhub/MyProject/venv/lib/python3.12/site-packages/daphne/management/commands/runserver.py", line 29, in get_default_application
    module = importlib.import_module(path)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/home/corey-james/Arborhub/MyProject/MyProject/asgi.py", line 12, in <module>
    import arborchat.routing
  File "/home/corey-james/Arborhub/MyProject/arborchat/routing.py", line 3, in <module>
    from . import consumers
  File "/home/corey-james/Arborhub/MyProject/arborchat/consumers.py", line 2, in <module>
    from channels.consumer import AsyncConsumer
ModuleNotFoundError: No module named 'channels.consumer'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.12/threading.py", line 1075, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.12/threading.py", line 1012, in run
    self._target(*self._args, **self._kwargs)
  File "/home/corey-james/Arborhub/MyProject/venv/lib/python3.12/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/home/corey-james/Arborhub/MyProject/venv/lib/python3.12/site-packages/daphne/management/commands/runserver.py", line 128, in inner_run
    application=self.get_application(options),
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/corey-james/Arborhub/MyProject/venv/lib/python3.12/site-packages/daphne/management/commands/runserver.py", line 153, in get_application
    return ASGIStaticFilesHandler(get_default_application())
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/corey-james/Arborhub/MyProject/venv/lib/python3.12/site-packages/daphne/management/commands/runserver.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 'MyProject.asgi'

Here is my consumers.py, you can see I clearly remove StopCOnsumers and added the line of code you suggested

import json
from channels.consumer import AsyncConsumer

from channels.generic.websocket import AsyncJsonWebsocketConsumer
import base64


# Channel consumer for both chat room group

class ChatConsumer(AsyncJsonWebsocketConsumer):
    async def connect(self):
        self.room_name = self.scope['url_route']['kwargs']['room_name']
        self.room_group_name = 'chat_%s' % self.room_name
        
        # Join room group
        await self.channel_layer.group_add(self.room_group_name, self.channel_name)
        
        await self.accept()
        
        
    async def disconnect(self, close_code):

        # Leave room group
        await self.channel_layer.group_discard(self.room_group_name, self.channel_layer)
        
    # Receive message from WebSocket
    
    async def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']
        
        # Send message to room group
        await self.channel_layer.group_send(
            self.room_group_name, {'type': 'chat.message', 'message': message}
        )
        
    # Receive message from room group
    
    async def re(self, event):
        message = event['message']
        
        # send message to websocket
        await self.send(text_data=json.dumps({'message': message}))
        
        
# Channel Consumer that consumed events for uploading/receiving image files in the message room

class UploadImageConsumer(AsyncJsonWebsocketConsumer):
    async def connect(self):
        await self.accept()
        

    async def disconnect(self, close_code):

        async def receive(self, bytes_data=None, text_data=None):
            if bytes_data:
            # Handles image file chunks (binary data)
            
                with open('image.jpg', 'rb') as f:
                    fcontent = f.read()
                await self.send(base64.b64encode(fcontent).decode('utf-8'))
            elif text_data:
                # Handles metadata (text data)
            
                await self.send(text_data=json.dumps({'message': text_data}))
            
            
    async def send_image(self, event):
        image_data = event['image_text']
        await self.send(bytes_data=image_data)

I’m not able to run python manage.py runserver. I was able to run python manage.py migrate successfully. I even changed ASGI_APPLICATION = "MyProject.asgi.application" to ASGI_APPLICATION = "routing.application" which didn’t work. Here is the error I get

  show_sunset_warning()
System check identified no issues (0 silenced).
March 29, 2025 - 17:52:24
Django version 5.1.7, using settings 'MyProject.settings'
Starting ASGI/Daphne version 4.1.2 development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/home/corey-james/Arborhub/MyProject/venv/lib/python3.12/site-packages/daphne/management/commands/runserver.py", line 29, in get_default_application
    module = importlib.import_module(path)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/home/corey-james/Arborhub/MyProject/MyProject/asgi.py", line 12, in <module>
    import arborchat.routing
  File "/home/corey-james/Arborhub/MyProject/arborchat/routing.py", line 3, in <module>
    from . import consumers
  File "/home/corey-james/Arborhub/MyProject/arborchat/consumers.py", line 2, in <module>
    from channels.consumer import AsyncConsumer
ModuleNotFoundError: No module named 'channels.consumer'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.12/threading.py", line 1075, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.12/threading.py", line 1012, in run
    self._target(*self._args, **self._kwargs)
  File "/home/corey-james/Arborhub/MyProject/venv/lib/python3.12/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/home/corey-james/Arborhub/MyProject/venv/lib/python3.12/site-packages/daphne/management/commands/runserver.py", line 128, in inner_run
    application=self.get_application(options),
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/corey-james/Arborhub/MyProject/venv/lib/python3.12/site-packages/daphne/management/commands/runserver.py", line 153, in get_application
    return ASGIStaticFilesHandler(get_default_application())
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/corey-james/Arborhub/MyProject/venv/lib/python3.12/site-packages/daphne/management/commands/runserver.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 'MyProject.asgi'

Here is my consumers.py, you can see I clearly remove StopCOnsumers and added the line of code you suggested

import json
from channels.consumer import AsyncConsumer

from channels.generic.websocket import AsyncJsonWebsocketConsumer
import base64


# Channel consumer for both chat room group

class ChatConsumer(AsyncJsonWebsocketConsumer):
    async def connect(self):
        self.room_name = self.scope['url_route']['kwargs']['room_name']
        self.room_group_name = 'chat_%s' % self.room_name
        
        # Join room group
        await self.channel_layer.group_add(self.room_group_name, self.channel_name)
        
        await self.accept()
        
        
    async def disconnect(self, close_code):

        # Leave room group
        await self.channel_layer.group_discard(self.room_group_name, self.channel_layer)
        
    # Receive message from WebSocket
    
    async def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']
        
        # Send message to room group
        await self.channel_layer.group_send(
            self.room_group_name, {'type': 'chat.message', 'message': message}
        )
        
    # Receive message from room group
    
    async def re(self, event):
        message = event['message']
        
        # send message to websocket
        await self.send(text_data=json.dumps({'message': message}))
        
        
# Channel Consumer that consumed events for uploading/receiving image files in the message room

class UploadImageConsumer(AsyncJsonWebsocketConsumer):
    async def connect(self):
        await self.accept()
        

    async def disconnect(self, close_code):

        async def receive(self, bytes_data=None, text_data=None):
            if bytes_data:
            # Handles image file chunks (binary data)
            
                with open('image.jpg', 'rb') as f:
                    fcontent = f.read()
                await self.send(base64.b64encode(fcontent).decode('utf-8'))
            elif text_data:
                # Handles metadata (text data)
            
                await self.send(text_data=json.dumps({'message': text_data}))
            
            
    async def send_image(self, event):
        image_data = event['image_text']
        await self.send(bytes_data=image_data)
Share Improve this question edited Mar 29 at 18:13 Cordev87 asked Mar 28 at 18:20 Cordev87Cordev87 112 bronze badges 2
  • 1 There's an ImportError halfway through the stack trace that you should address. Make sure StopConsumer is available to import from channels.exceptions – schwartz721 Commented Mar 28 at 22:52
  • You are having an improperly configured error in your traceback error stack. Can you help us with your asgi, static/media files? Have you ensured that your project points to the ASGI_APPLICATION instance in your settings? – Blaisemart Commented Mar 29 at 7:21
Add a comment  | 

1 Answer 1

Reset to default 0

I fixed it finally, the problem was that Redis wasn't installed

发布评论

评论列表(0)

  1. 暂无评论