Server error (500) Django deployment on heroku

2019-09-01 03:15发布

I turned debug to False in my settings.py file (note that before I turned it to false, everything was workign perfectly) and when I ran git push heroku master and went to my website, the home page was working but that wasn't the case for all of my pages: on some, I got an error saying Server Error (500)

Here is my settings.py code :

DEBUG = False
ALLOWED_HOSTS = ["hacka-labs.herokuapp.com"]

What I have noticed is that the urls where I pass an id aren't working

Please help me if you know the answer

1条回答
何必那么认真
2楼-- · 2019-09-01 03:40

I had a similar problem and it's hard to figure out when DEBUG is set to False. When I reconfigured it back to DEBUG = True everything worked fine again. So the problem was

You don't generally see this error in development because when DEBUG = True, ManifestStaticFilesStorage switches to non-hashed urls. https://stackoverflow.com/a/51060143/7986808

The solution to the problem in your case may differ from mine. But you can find the problem by changing the logging method of your Django project, so you can see more info in the command line when running heroku logs -t -a <heroku-app> even in DEBUG = False mode.

Change your django logging settings in settings.py as follow:

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': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        }
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'django.request': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': False,
        },
    }
}

Then run heroku logs -t -a <heroku-app> and open the url where you previously got 500 Error you will find out the problem in logs. Hope this will help you.


In case there are some static files missing just switch your static root in settings.py as follow STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles'). Running heroku run python manage.py collectstatic -a <heroku-app> creates staticfiles directory.

查看更多
登录 后发表回答