Reputation: 73
I am able to deploy my app (via Django) to Heroku when DEBUG = True but when DEBUG = False I get a Server Error. I think it has to do with how I've set up my static files. When I comment out "STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'" I don't get an error message but the site is completely unformatted. Here's the relevant settings.py code:
INSTALLED_APPS = [
...
'django.contrib.staticfiles',
...
]
ALLOWED_HOSTS = ['*']
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT= os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
I've added my static folder in all sorts of locations (same folder as settings.py, in the root folder, etc) to no avail.
Any ideas?
Upvotes: 3
Views: 1693
Reputation: 1
I've been facing the exact same problem but it's not the staticfiles its DEBUG = False
that's causing the issue. When I realised it I remembered I visited a github repo yesterday that had the solution to my problem.
https://github.com/8sagh8/DjangoRestApi-part1-youtube-project/blob/main/README.md
You could keep changing DEBUG
to False
or True
between production and development or you could use the code below
import sys
if (len(sys.argv) >= 2 and sys.argv[1] == 'runserver'):
DEBUG = True
else:
DEBUG = False
If you've worked with C programs this would look quite familiar, the code above checks if you typed in runserver
at the end of python manage.py runserver
and sets DEBUG
to True
but else it will set DEBUG
to True
Upvotes: 0
Reputation: 3799
You may have forgotten to include the whitenoice middleware in your settings.py MIDDLEWARE setting. Edit your settings.py file and add WhiteNoise to the MIDDLEWARE_CLASSES list, above all other middleware apart from Django’s SecurityMiddleware:
MIDDLEWARE_CLASSES = [
# 'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
#...
]
More information can be found in the whitenoise docs: http://whitenoise.evans.io/en/stable/django.html
Go through the step-by-step setup to see what you're missing. The Heroku docs tend to omit the middleware addition--which causes the bug--and perhaps there's something else missing for your application.
Upvotes: 1
Reputation: 73
Those staticfile settings came directly from the Heroku website (https://devcenter.heroku.com/articles/django-assets). When I ran python manage.py collectstatic
I got a key error 'DATABASE_URL' which I had to export
a value for and then when I pushed my files to Heroku the website worked properly.
Upvotes: 0