简体   繁体   中英

Server error (500) when trying to deploy django app with DEBUG=False on heroku

I am trying to deploy an django application on heroku. when the DEBUG variable is False, I receive a server error (500). Everything works fine when DEBUG is True. How to solve this problem? Here is some of the logs:

2021-08-17T10:15:08.603841+00:00 app[web.1]: 10.1.59.205 - - [17/Aug/2021:10:15:08 +0000] "GET /connexion?next=/ HTTP/1.1" 500 145 "-" "Mozilla/5.0 (Windows NT 10.0;
Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.67"
2021-08-17T10:15:08.603943+00:00 heroku[router]: at=info method=GET path="/connexion?next=/" host=pnl-testapi.herokuapp.com request_id=edc211c1-244d-49ad-aac6-6e2c1d9
cf5c0 fwd="160.119.178.105" dyno=web.1 connect=0ms service=63ms status=500 bytes=403 protocol=https

Did you add your domain in the ALLOWED_HOSTS variable in settings.py?

ALLOWED_HOSTS = ['.herokuapp.com']

Edit: You have added the access log of the webserver, please also add the exception. Have you tried running your project locally so you have quick and easy access to the console?

Fix for Server error (500) when trying to deploy django app with DEBUG=False on heroku

pip install whitenoise

Edit your settings.py file and add WhiteNoise to the MIDDLEWARE list, above all other middleware apart from Django's SecurityMiddleware:

MIDDLEWARE = [
  # 'django.middleware.security.SecurityMiddleware',
  'whitenoise.middleware.WhiteNoiseMiddleware',
  # ...
]

If you install django-heroku , try to comment it out and also comment django_heroku.settings(locals()) out.

You can turn your DEBUG = False and redeploy to Heroku. This method actually worked for me. You can give it a try.

You can also read whitenoise documentation: http://whitenoise.evans.io/en/stable/

As a rule of thumb this error is generated due to 3 things:

1.- You did not define the path for your static file directories

2.- You did not especify ALLOWED_HOST

3.- You did not install whitenoise


to solve this you have to install whitenoise and django-heroku in Python:

pip install whitenoise

pip install django-heroku


and add this in your settings.py:

import django_heroku # import django-heroku


ALLOWED_HOSTS = ['*'] # add your host(s)

# Add whitenoise in MIDDLEWARE
MIDDLEWARE = [
    ...
    'whitenoise.middleware.WhiteNoiseMiddleware', # add this
]

# Add your paths for 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' # add this

django_heroku.settings(locals()) # add this

With this you need to write:

heroku config:set DISABLE_COLLECTSTATIC=0

Of this way heroku will be able to identify your static file directories.


It seems that heroku need to have reference to static file directories obligatorily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM