Search code examples
pythonmysqldjangosettingskeyerror

How to get an app running on Heroku with Python Django on MySql? KeyError: '', code=H10 desc="App crashed"


I have been trying to get my first application on the web. Using as cloud application platform Heroku. The app is built in Python with Django and is meant to use a MYSQL database for which is provided as add_on JAWSDB. The url of the JAWSDB is set to the DATABASE_URL. My app code is public on GitHub . I am able to deploy the code on Heroku but get an application error when I open the app. (See log below). I think it has to do with my_settings, honestly I don't know what is to be corrected.

  1. Is it the database that get not opened? at=error code=H10 desc="App crashed" method=GET path="/" host=learningnow.herokuapp.comHowever it is possible to connect to the remote database via MySql workbench.
  2. The log is referring to

engine = SCHEMES[url.scheme] if engine is None else engine KeyError: ''

Are the

django_heroku.settings(locals())

for which I assume the DATABASE_URL is found, but it seems not working.

  1. Has it to do with the config = parse(s, engine, conn_max_age, ssl_require). However in my last attempt i have put

    DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': dj_database_url.config(default=os.environ[DATABASE_URL], ssl_require=False), }

2019-03-02T15:33:06.132322+00:00 app[web.1]: engine = SCHEMES[url.scheme] if engine is None else engine 2019-03-02T15:33:06.132370+00:00 app[web.1]: KeyError: '' 2019-03-02T15:33:07.351885+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=learningnow.herokuapp.com request_id=28373f9d-99b1-4362-8054-de2e72aac130 fwd="81.207.96.28" dyno= connect= service= status=503 bytes= protocol=https 2019-03-02T15:33:08.147290+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=learningnow.herokuapp.com request_id=073e5f94-a1ce-41ea-979c-4dabb341ea86 fwd="81.207.96.28" dyno= connect= service= status=503 bytes= protocol=https

See the last part of my_settings.py file which is meant to connect and open the app on Heroku:

# Heroku settings

if os.getcwd() == '/app': import django_heroku django_heroku.settings(locals())

import dj_database_url

DATABASES = {
    'default': {
                'ENGINE': 'django.db.backends.mysql',
                'NAME': dj_database_url.config(default=os.environ[DATABASE_URL],
                ssl_require=False),
    }
}

# Honor the 'X-Forwarded-Proto' header for request.is_secure().
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Allow all host headers.
ALLOWED_HOSTS = ['*']

# Static asset configuration
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

Any suggestions on how to get this better?


Solution

  • Problem solved, several code changes needed to happen. See enter link description here

    for a full working application for Heroku, connected to a JawsDB add-on (MySql) database using the framework of Python with Django.

    Esspecially the connection to the JawsDb had to be set: See last part of the my_settings.txt file:

    if os.getcwd() == '/app':
    import django_heroku
    
    DEBUG = True
    ALLOWED_HOSTS = ['learningnow.herokuapp.com']
    
    DATABASE_URL = os.getenv('DATABASE_URL')
    SECRET_KEY = os.getenv('SECRET_KEY')
    
    database_attr = DATABASE_URL.split(':')
    
    JaName = database_attr[3].split('/')[1].rstrip("'")
    JaUser = database_attr[1].lstrip('//')
    JaPwrd = database_attr[2].split('@')[0]
    JaHost = database_attr[2].split('@')[1]
    JaPort = int(database_attr[3].split('/')[0])
    
    DATABASES = { 
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': JaName,
            'USER': JaUser,
            'PASSWORD': JaPwrd,
            'HOST': JaHost,
        }
    }`