Serkan
Serkan

Reputation: 81

I get an error when I want to save messages in a real-time chat application in Django

I don't get any error like this

import json
from channels.generic.websocket import AsyncWebsocketConsumer
from channels.db import database_sync_to_async
from .models import ChatRoom

class ChatConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.room_name = self.scope\['url_route'\]\['kwargs'\]\['room_name'\]
self.room_group_name = f'chat\_{self.room_name}'

        await self.create_or_update_room(self.room_name)
        
        await self.channel_layer.group_add(
            self.room_group_name,
            self.channel_name
        )
    
        await self.accept()
    async def disconnect(self, close_code):
        # Odayı terk et
        await self.channel_layer.group_discard(
            self.room_group_name,
            self.channel_name
        )
    
    async def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']
    
        await self.channel_layer.group_send(
            self.room_group_name,
            {
                'type': 'chat_message',
                'message': message
            }
        )
    async def chat_message(self, event):
        message = event['message']
      
        await self.send(text_data=json.dumps({
            'message': message
        }))
    @database_sync_to_async
    def create_or_update_room(self, room_name):
            room, created = ChatRoom.objects.get_or_create(name=room_name)

(env) PS

C:\Users\Serkan\Desktop\stock_trackings\stock_tracking> daphne -p 8000 stock_tracking.asgi:application "C:\Users\Serkan\Desktop\stock_trackings\env\Lib\site-packages\django\apps\registry.py", line 260, in get_containing_app_config self.check_apps_ready() File "C:\Users\Serkan\Desktop\stock_trackings\env\Lib\site-packages\django\apps\registry.py", line 138, in check_apps_ready raise AppRegistryNotReady("Apps aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

I get an error when I checked the settings, I checked the applications, and when I continue without saving the messages, it works without any problems.

Upvotes: 0

Views: 50

Answers (0)

Related Questions