Florian Buchfink
Florian Buchfink

Reputation: 127

Django Heroku Server Error page 500 only when Debug = False , When Debug on True everything works fine. Why?

My Djangoproject call: myDebug and my Django app call: Deb

it is on Heroku: meindebug.herokuapp.com

settings.py

import os
import django_heroku 

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))



SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'


DEBUG = False


ALLOWED_HOSTS = ['meindebug.herokuapp.com']

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'Deb',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    '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',
    'whitenoise.middleware.WhiteNoiseMiddleware',
]

ROOT_URLCONF = 'myDebug.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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 = 'myDebug.wsgi.application'

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


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


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

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

django_heroku.settings(locals())

!!! please notice i tried it with ALLOWED_HOSTS = ['*'] but nothing changed !!!

wsgi.py

import os

from django.core.wsgi import get_wsgi_application
from whitenoise import WhiteNoise

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

application = get_wsgi_application()
application = WhiteNoise(application, root='/static/Deb/Images/')
application.add_files('/static/Deb/Images/', prefix='more-files/')

Procfile

web: gunicorn myDebug.wsgi 

Pipfile

[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"

[packages]
gunicorn = "*"
django-heroku = "*"
whitenoise = "*"

[dev-packages]

[requires]
python_version = "3.6"

Since two Weeks i tried to fix that issue. Unfortunately, I was not successful.

Nowhere i find a real Fix. Only some words there and there but noone have a real Solution.

Upvotes: 1

Views: 2260

Answers (3)

Nitanshu Bajpai
Nitanshu Bajpai

Reputation: 1

I had a same issue it appears that I had some css file linked but the original files were deleted. please make sure you aren't linking any not existing external file.

Upvotes: 0

Marcox
Marcox

Reputation: 102

For what it's worth, you'll face the same issue if you have an html comment in your code that calls the template tag "static" (probably the behavior is the same for every commented template tag), this was my case:

<!-- <img  src="{% static 'main_website/images/image.jpg' %}" alt=""> -->

removing the comment resoled the 500 error (which also, was not being logged).

Upvotes: 1

Florian Buchfink
Florian Buchfink

Reputation: 127

First you have to run python manage.py collectstatic. Then in Heroku the Config Variables to COLLECTSTATIC = 1.

but the file Path to your Pictures/Files have to show like this

{% static "MyApp/Images/Picture.png" %}

you get an Error if you write it so

 {% static "/MyApp/Images/Picture.png" %}

the slash at front of MyApp is the problem. So write it without slash then it works.

If you have a favicon in the head then you have to write it so

<link rel="shortcut icon" href="https://www.yourwebsite.de/static/MyApp/Images/favicon.ico">

Upvotes: 1

Related Questions