3

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://:xxxxxxxx@redis-13131.c85.us-east-1-2.ec2.cloud.redislabs.com: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!

Animesh Timsina
  • 386
  • 2
  • 10

1 Answers1

0

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.

hassanzadeh.sd
  • 3,091
  • 1
  • 17
  • 26
  • I have all the apps in INSTALLED_APPS installed on my computer. Also, I haven't created any virtual environment for my project. – Animesh Timsina Jul 12 '19 at 18:02
  • this error means your result client is not running, i think you shoud atention your comma in arrap installed_apps. Or update your asgi configuration again and try it. – hassanzadeh.sd Jul 13 '19 at 09:47
  • and ensure that you are not importing models or similar app code, in __init__.py files of your apps. – hassanzadeh.sd Jul 13 '19 at 09:53