3
votes

I have deployed my django webapp to my heroku server and it was working fine until I added a websocket connection that shows the contents of a model object in a separate url as soon as that object is created. For this, I used Django channels with a redis server hosted on redislabs. To run asgi app, I tried to use daphne server but when I try to run the daphne server with the following command: $daphne smartResturant.asgi:channel_layer --port 8888 , it says
"django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet"

My asgi.py

import os
import django
from smartResturant.routing import get_default_application


os.environ.setdefault("DJANGO_SETTINGS_MODULE", "smartResturant.settings")
django.setup()
application = get_default_application()

My settings.py

ASGI_APPLICATION = 'smartResturant.routing.application'
CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {

            "hosts": ['redis://:[email protected]:13131']     
        },

    },
}

It works fine when I run locally without using daphne server. But I found out that to run asgi based apps on a hosted server, you have to use daphne server and I am unable to run it. Any help will be appreciated!

1
first the most basic question, do you forget to add django-secret-key in your settings ? - Shakil
I have it under my settings! - Animesh Timsina
In my past experience i do encounter this problem for circular dependency, not sure about your though. hope more experience person will come to help on this. - Shakil
Did you find the solution to this? - Sabito 錆兎

1 Answers

0
votes

This error may occur when you are adding an app in INSTALLED_APPS in the settings.py file but you do not have that app installed in your computer. You have two solution:

  1. Install that app using package managers like pip in django
  2. Or Comment out that installed app in the settings.py file

This error may also arise if you are not in your virtual environment which you may have created for your project.

When you run commands like python manage.py runserver, django automatically runs django.setup for you using DJANGO_SETTINGS_MODULE environment variable. So the code in views.py can access models, because django ensures that django.setup is called before views are imported. Since you are running your shell script as a simple python file, so you must manually call django.setup.