Madiyor
Madiyor

Reputation: 811

Server Error (500) in django deploying on heroky when debug=False

When I run with DEBUG=True everything works fine but when I set DEBUG = False I have only Server Error 500. I am using the latest Django version (3.x.x) and deploying my project to heroku. I have no idea why this is happening.

My settings.py file (some parts) as follows

import django_heroku
import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

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

ALLOWED_HOSTS = ['research-and-development.herokuapp.com',
                 'localhost', '127.0.0.1']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}
# Application definition

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'corsheaders.middleware.CorsPostCsrfMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

# static and media files and urls set-up
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# end of static and media files and url set-up

django_heroku.settings(locals())

Please help me to solve this problem.

Upvotes: 1

Views: 329

Answers (1)

Madiyor
Madiyor

Reputation: 811

I do not know whether it is the best answer or not. I have tried a lot of things. Eventually I removed django_heroku. I followed the django heroku docs and my settings.py (some part) is as follows.

DEBUG = False

ALLOWED_HOSTS = ['research-and-development1.herokuapp.com',
                 'localhost', '127.0.0.1', '0.0.0.0', ]

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

DATABASES['default'].update(dj_database_url.config(conn_max_age=600, ssl_require=True))

MIDDLEWARE = [
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'corsheaders.middleware.CorsPostCsrfMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]


# static and media files and urls set-up
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

Before I push I have created psql database on heroku using the following command. heroku addons:create heroku-postgresql:hobby-dev and I pushed with collected static files which are generated using python manage.py collectstatic. Have fun coding.

Upvotes: 2

Related Questions