Deploying Django to Heroku - Server Error (500)
I Have Been Making a Django App, and Am Now Trying to Deploy It to Heroku. However When I Go on It, Says Server Error (500), and the Log Says...
I have been making a django app, and am now trying to deploy it to heroku.
However when I go on it, says Server Error (500), and the log says: 2017-05-27T21:00:14.634310+00:00 heroku[router]: at=info method=GET path="/" host=remberit.herokuapp.com request_id=065d27c6-9211-458f-9fc6-bb677d43581e fwd="86.13.204.65" dyno=web.1 connect=0ms service=151ms status=500 bytes=387 protocol=https
Here is my settings.py (at learst the relevant parts, but please ask if you would like the rest):
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, "staticfiles")
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
import dj_database_url
DATABASES['default'] = dj_database_url.config()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
ALLOWED_HOSTS = ['*']
DEBUG = False
try:
from .local_settings import *
except ImportError:
pass
And here is my wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
#from whitenoise.django import DjangoWhiteNoise
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'remberit.settings')
django.setup()
application = get_wsgi_application()
#application = DjangoWhiteNoise(application)
Here is my Procfile:
web: gunicorn remberit.wsgi
Here is my runtime.txt:
python-3.5.2
Here is my requirements.txt:
appdirs==1.4.3
dj-database-url==0.4.2
gunicorn==19.7.1
packaging==16.8
pyparsing==2.2.0
six==1.10.0
whitenoise==3.3.0
psycopg2==2.6.2
And here is the output of pip freeze:
appdirs==1.4.3
dj-database-url==0.4.2
gunicorn==19.7.1
packaging==16.8
pyparsing==2.2.0
six==1.10.0
whitenoise==3.3.0
Also, when I run the app locally with gunicorn remberit.wsgi or python manage.py runserver it works fine, it only doesn't work when I use heroku.
Please tell me if you need anymore information.
7 Answers
Other solution that may solve your issue: Comment out or remover the GzipManifestStaticFilesStorage from whitenoise. For some reason this is not working well.
# STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
Found this solution on Why would django fail with server 500 only when Debug=False AND db is set to production database on Heroku?
As mustapha-belkacim said, you need to migrate your apps:
heroku run python manage.py migrate
I had a similar problem when I first deployed my app to heroku, in my case it was the database that needs migrations. So I had to run this commande:
heroku run -a your-app-name python manage.py migrate
take a look at this tutorial
Please provide (or check) your TEMPLATES variable from settings. Make sure the 'DIRS' value points to the right template folder where index.html (or base template) is.
I faced a similar error and this was the problem in my case.
As commented in other question (Django app deployed to Heroku producing Server error 500), I realized this after turning DEBUG to True in production for a brief moment to find better help on the issue. Then, back to False of course.
Does your /admin works? If so, this answer might help you. If not, there is a number of things that can go wrong, maybe others can help? (eg: Why is psycopg2 not outputted with pip freeze if you have it in requirements.txt? Is it installed? Are you working with postgreSQL on both local and production environments?)
I also faced the same problem so the best and quickest solution is to remove this line:
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
and add this:
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
A 500 error could be a lot of possible things. Set DEBUG=True temporarily to get more information.
Make sure you aren't linking any non existing external file, like in my case I had few css files linked with my html but the actual files were deleted.