在HEROKU上部署Django ASGI(DAPHNE)时遇到问题

t3irkdon  于 2022-11-13  发布在  Go
关注(0)|答案(1)|浏览(194)

过去几天我一直在试着部署但就是没能成功在heroku上,它说应用程序已部署,但当我进入日志时,我看到了错误。我尝试打开应用程序(例如,管理页面),我得到了应用程序错误。我尝试在导入任何其他内容之前调用get_asgi_application,但没有工作。以下是我收到的错误:错误

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

我的asgi文件:

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
        )
    ),
})

我的程序文件:

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

设置文件:

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()
....

我试过添加. import django,然后调用django.setup()。然而同样的错误不断出现。任何帮助都将不胜感激
解决了!
根据Iain Shelvington在评论中的解决方案!!

imzjd6km

imzjd6km1#

尝试将行chat.routing import *放在对get_asgi_application()的调用之后,您不需要调用django.setup()

import os
import django
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application

django_asgi_app = get_asgi_application()

//it is important to import after calling get_asgi_application()
from chat.routing import *

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Grouplisten.settings")

application = ProtocolTypeRouter({
  "http": django_asgi_app,
  "websocket": AuthMiddlewareStack(
    URLRouter(
        websocket_urlpatterns
    )
 ),
 })

相关问题