[Solved]-Django Heroku Server Error (500) when I set Debug – False on True it is working fine

8👍

I think I got it. The solution is working. But I think that it’s also very weird. I would appreciate if someone could comment why is it like that.

You can leave whitenoise, you can make a little bit tidying up with static files dirs. Make it look like that:

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


STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

But it isn’t necessary I think. (It is also best to keep your “static” folder in your main project folder).

But The big fix is (working on django 3):

You need to run

python manage.py collectstatic

Locally ! And then submit it to heroku. And then, everything is working with Debug set to False. I tried to keep collectstatic just with .keep file. I tried to commit only .json hoping that it will be repopulated with new collectstatic files. But no. It is working only if I run collectstatic locally and then commit EVERYTHING to heroku.

If someone have more understanding of this issue and could explain it to me I would be really grateful.

In the mean time I hope that someone else will also benefit from this solution.

I found answer on this question – ValueError: Missing staticfiles manifest entry for 'favicon.ico' . Thank you very much emazzotta, also I didn’t have to get rid of whitenoise and It is working in django3.

👤Peksio

6👍

Debug your django app by logs by typing this, heroku logs --tail -a your_herokuapp_name or https://dashboard.heroku.com/apps/your_herokuapp_name/logs.

Before that add this in your settings.py:

 # Debugging in heroku live
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': ('%(asctime)s [%(process)d] [%(levelname)s] ' +
                       'pathname=%(pathname)s lineno=%(lineno)s ' +
                       'funcname=%(funcName)s %(message)s'),
            'datefmt': '%Y-%m-%d %H:%M:%S'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        }
    },
    'handlers': {
        'null': {
            'level': 'DEBUG',
            'class': 'logging.NullHandler',
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        }
    },
    'loggers': {
        'testlogger': {
            'handlers': ['console'],
            'level': 'INFO',
        }
    }
}

DEBUG_PROPAGATE_EXCEPTIONS = True
COMPRESS_ENABLED = os.environ.get('COMPRESS_ENABLED', False)

This will show you error with debug=False in heroku logs.

STACKOVERFLOW QUESTION [SOURCE]

0👍

ValueError: Missing staticfiles manifest entry for 'builder/logo.png'

It seems that in builder/staticfiles(/builder), logo.png is referenced but doesn’t exist. It might be that it’s trying to get to static rather than staticfiles, in which case that seems to be an internal error that would be patched out in a later version of Django. Seeing as you’re using Django 2, you should probably upgrade to Django 3 if adding logo.png doesn’t work.

TL;DR: Add logo.png to builder/staticfiles(/builder) or upgrade Django.

0👍

I had the same problem and in my case, I was linking to a css file that didn’t exist.

For example:

{% block head %}
    <link rel="stylesheet" href="{% static 'css/my-styles.css' %}">
{% endblock %}

Whereas there was actually no my-styles.css file.

I have removed the link and after that the app started to work with both settings of DEBUG (True and False).

0👍

okay so it’s probably because you left out some important imports. when DEBUG = False django will not handle your static files so we need our remote server to handle that. This is the process to fix that:

  1. go to your project directory (where manage.py exists) in your command line.

  2. pip install whitenoise then make sure it is added to your requirements.txt by doing pip freeze > requirements.txt if this is already there’s no problem.

  3. go to your settings.py folder and locate MIDDLEWARE. add 'whitenoise.middleware.WhiteNoiseMiddleware', to it.

  4. in the same settings.py add STATIC_ROOT = os.path.join(BASE_DIR, 'static') for my case it was just above django_heroku.settings(locals()) note: the ‘static’ there is just the name of the directory where your static files are stored.

  5. now go to your wsgi.py file in the same directory as settings.py type the following at the top from whitenoise import WhiteNoise also at the bottom you will use the import so add application = WhiteNoise(application)

  6. now change DEBUG = False and go back to the command prompt in the project directory

  7. type python manage.py collectstatic after this a new folder will appear in the root directory of your project . Just leave it there

  8. do git add . and git commit -m "description"

  9. push to your remote server. for example git push heroku master for heroku

I hope this helps

Leave a comment