Reputation: 43
I am trying to deploy a website to Heroku and I'm running into some trouble with Whitenoise and serving media when DEBUG=False.
I have gotten the site to work when DEBUG is on but after adding Whitenoise the server throws and I/O error when trying to serve files that I know are there, even is DEBUG is on.
My static files are configured as
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, '../pages/static'),
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
When I try to load the front page, which contains an image that I know is stored in 'media' I get
IOError at /
[Errno 2] No such file or directory: u'/app/{appname}/media/{picture}.png'
This is the first app I've ever deployed with Heroku and the first website I've written with Django so I'm still trying to understand how it all works. I thought I should be able to add the Whitenoise middlewear to the static files settings I had during development and it should pretty much just work but clearly I'm doing something wrong.
Upvotes: 0
Views: 267
Reputation: 43
I figured it out. WhiteNoise is as simple as adding the few lines mentioned in the documentation, my problem was that I had changed something about my static file storage and distribution between when it worked and when I added those lines.
In the end I went with AWS S3 because it can serve static and media files. I also had a ton of issues there because I needed to set my host and include my region but that's all solved.
Upvotes: 0
Reputation: 141
Is this an error you are getting when running heroku local or on the deployed site?
If this is happening when running the site locally, staticfiles are served differently. This is from Django docs:
During development, if you use django.contrib.staticfiles, this will be done automatically by runserver when DEBUG is set to True (see django.contrib.staticfiles.views.serve()).
This method is grossly inefficient and probably insecure, so it is unsuitable for production.
So when debug is False you need to change settings.py to link to remote staticfiles. This might take a bit of research, I find it easier to just set debug to true.
Alternatively on the deployed heroku site I had this kind of error from faulty static tags in my templates, such as incorrect file locations, which only caused errors with debugging switched off.
Upvotes: 0