Search code examples
pythondjangoheroku

Django Project works on local but not on heroku


If I try to run the server on local it works without errors but when trying to run it with heroku I get

2021-08-24T23:55:05.395710+00:00 app[web.1]: Internal Server Error: /
2021-08-24T23:55:05.395718+00:00 app[web.1]: Traceback (most recent call last):
2021-08-24T23:55:05.395719+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
2021-08-24T23:55:05.395720+00:00 app[web.1]:     return self.cursor.execute(sql, params)
2021-08-24T23:55:05.395720+00:00 app[web.1]: psycopg2.errors.UndefinedTable: relation "main_chapter" does not exist
2021-08-24T23:55:05.395721+00:00 app[web.1]: LINE 1: ...er_text", "main_chapter"."chapter_footnotes" FROM "main_chap...
2021-08-24T23:55:05.395722+00:00 app[web.1]:                                                              ^
2021-08-24T23:55:05.395723+00:00 app[web.1]: 
2021-08-24T23:55:05.395723+00:00 app[web.1]: 
2021-08-24T23:55:05.395723+00:00 app[web.1]: The above exception was the direct cause of the following exception:
2021-08-24T23:55:05.395724+00:00 app[web.1]: 
2021-08-24T23:55:05.395724+00:00 app[web.1]: Traceback (most recent call last):
2021-08-24T23:55:05.395724+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
2021-08-24T23:55:05.395725+00:00 app[web.1]:     response = get_response(request)
2021-08-24T23:55:05.395725+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
2021-08-24T23:55:05.395726+00:00 app[web.1]:     response = wrapped_callback(request, *callback_args, **callback_kwargs)
2021-08-24T23:55:05.395726+00:00 app[web.1]:   File "/app/main/views.py", line 10, in home
2021-08-24T23:55:05.395726+00:00 app[web.1]:     last = chapter_list.last()
2021-08-24T23:55:05.395727+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/query.py", line 679, in last
2021-08-24T23:55:05.395727+00:00 app[web.1]:     for obj in (self.reverse() if self.ordered else self.order_by('-pk'))[:1]:
2021-08-24T23:55:05.395727+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/query.py", line 280, in __iter__
2021-08-24T23:55:05.395728+00:00 app[web.1]:     self._fetch_all()
2021-08-24T23:55:05.395728+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/query.py", line 1324, in _fetch_all
2021-08-24T23:55:05.395728+00:00 app[web.1]:     self._result_cache = list(self._iterable_class(self))
2021-08-24T23:55:05.395729+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/query.py", line 51, in __iter__
2021-08-24T23:55:05.395729+00:00 app[web.1]:     results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
2021-08-24T23:55:05.395729+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1175, in execute_sql
2021-08-24T23:55:05.395730+00:00 app[web.1]:     cursor.execute(sql, params)
2021-08-24T23:55:05.395730+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute
2021-08-24T23:55:05.395730+00:00 app[web.1]:     return super().execute(sql, params)
2021-08-24T23:55:05.395730+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
2021-08-24T23:55:05.395731+00:00 app[web.1]:     return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
2021-08-24T23:55:05.395732+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
2021-08-24T23:55:05.395732+00:00 app[web.1]:     return executor(sql, params, many, context)
2021-08-24T23:55:05.395732+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
2021-08-24T23:55:05.395733+00:00 app[web.1]:     return self.cursor.execute(sql, params)
2021-08-24T23:55:05.395733+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
2021-08-24T23:55:05.395733+00:00 app[web.1]:     raise dj_exc_value.with_traceback(traceback) from exc_value
2021-08-24T23:55:05.395733+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
2021-08-24T23:55:05.395734+00:00 app[web.1]:     return self.cursor.execute(sql, params)
2021-08-24T23:55:05.395734+00:00 app[web.1]: django.db.utils.ProgrammingError: relation "main_chapter" does not exist
2021-08-24T23:55:05.395734+00:00 app[web.1]: LINE 1: ...er_text", "main_chapter"."chapter_footnotes" FROM "main_chap...
2021-08-24T23:55:05.395734+00:00 app[web.1]:                                                              ^

I litteraly checked every template, forms, views couldn't find anything about main_chapter. I think maybe it is a problem about psycopg2 because when I tried to install it I got so many errors so I installed psycopg2-binary instead. If you require more files, please tell me so.


Solution

  • These are all the necessary settings Project/settings.py try use it

    import os
    from pathlib import Path
    import djoser
    import django_heroku
    import dj_database_url
    
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    
    SECRET_KEY = os.getenv('SECRET_KEY', 'Optional default value')
    
    DEBUG = False
    
    ALLOWED_HOSTS = []
    
    AUTH_USER_MODEL = 'api.userProfile'
    
    
    INSTALLED_APPS = [
        'api',
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'djoser',
        'rest_framework',
        'rest_framework_simplejwt',
        'rest_framework.authtoken',
        'corsheaders',
    ]
    
    MIDDLEWARE = [
        'corsheaders.middleware.CorsMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.security.SecurityMiddleware',
        'whitenoise.middleware.WhiteNoiseMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    ]
    
    CORS_ORIGIN_ALLOW_ALL = True
    ROOT_URLCONF = 'config.urls'
    
    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR, '../templates')]
            ,
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },
        },
    ]
    
    WSGI_APPLICATION = 'config.wsgi.application'
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': 'full_db'
        }
    }
    
    AUTH_PASSWORD_VALIDATORS = [
        {
            'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
        },
    ]
    
    LANGUAGE_CODE = 'en-us'
    
    TIME_ZONE = 'UTC'
    
    USE_I18N = True
    
    USE_L10N = True
    
    USE_TZ = True
    
    
    REST_FRAMEWORK = {
        'DEFAULT_AUTHENTICATION_CLASSES': (
            'rest_framework_simplejwt.authentication.JWTAuthentication',
        ),
    }
    
    # Heroku: Update database configuration from $DATABASE_URL.
    db_from_env = dj_database_url.config(conn_max_age=500)
    DATABASES['default'].update(db_from_env)
    
    # Static files (CSS, JavaScript, Images)
    
    PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
    
    STATIC_URL = '/static/'
    
    STATIC_ROOT = os.path.normpath(os.path.join(BASE_DIR, '../staticfiles'))
    
    STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
    
    MEDIA_URL = '/media/'
    
    MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media")