'Trouble with Django ASGI (DAPHNE) Deployment on HEROKU

i've been trying to deploy for the last couple days and I just can't seem to get it working: on heroku , it says application deployed but then when i go into the logs I see errors. I try opening up the app (for example, admin page) and I get application error. I've tried calling the get_asgi_application prior to importing anything else, that didn't work. Here are the errors i recieve: ERROR

   File "/app/.heroku/python/lib/python3.9/site-packages/django/apps/registry.py", line 136, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
 django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Process exited with status 1
 State changed from starting to crashed

at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=myappname.herokuapp.com request_id=18f76666-adff-40f8-83ae-55df56d78208 fwd="24.150.189.187" dyno= connect= service= status=503 bytes= protocol=https

my asgi file:


import os
from django.core.asgi import get_asgi_application
import django
#from channels.auth import AuthMiddlewareStack



#from channels.security.websocket import AllowedHostsOriginValidator
from channels.routing import ProtocolTypeRouter, URLRouter


#from .settings import ALLOWED_HOSTS
from myappnameapp.routing import *

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myappname.settings')
django.setup()


#from django.urls import path

django_asgi_app = get_asgi_application()
from myappname.auth_middleware import TokenAuthMiddleware

application = ProtocolTypeRouter({
    # (http->django views is added by default)
    "http": django_asgi_app,
    'websocket': TokenAuthMiddleware(
        URLRouter(
            websocket_urlpatterns
        )
    ),
})


my procfile:

release: python manage.py migrate
web: daphne myappname.asgi:application --port $PORT --bind 0.0.0.0 -v2
worker: python manage.py runworker channel_layer -v2

SETTINGS file:

import dj_database_url
from pathlib import Path
import os




# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ['127.0.0.1', 
                 'myappname.herokuapp.com', 'localhost']


# Application definition


INSTALLED_APPS = [
    'channels',

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    
    # 3rd party
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'rest_framework',
    'rest_framework.authtoken',
    'dj_rest_auth',
    'dj_rest_auth.registration',
    'corsheaders',
    'fcm_django',
    'allauth.socialaccount.providers.google',
    'allauth.socialaccount.providers.facebook',
    'allauth.socialaccount.providers.apple',
    'django_celery_results',
    'django_celery_beat',
    'storages',
    # local
    'myappnameapi',
    'accounts',
    'myappnameapp',


]
from firebase_admin import initialize_app
FIREBASE_APP = initialize_app()
....


i've tried adding. import django, and then calling django.setup() before everything. yet the same error keeps coming. any help would be appreciated

SOLVED!!

as per Iain Shelvington solution in the comments!!!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source