I’m trying to deploy my Django project to Heroku. In the staging pipeline environment, it runs and I can access my site. But when I promote the tested changes on Heroku to prod, the browser shows an “Internal Server Error”. When I run: $ heroku logs --tail
, the trace back reveals a 500 error. In greater detail, the trace back indicates: “ValueError: Missing staticfiles manifest entry for ‘books/Blair_2010.pdf’”. That’s the error message.
For context, books
is a directory carrying a very small collection of .pdf’s which are binary static files.
In the Heroku administrative dashboard, when I switch the DEBUG
flag to False
, Django serves my web pages no problem. When I click a link with a hard coded reference to a static file, it loads. Static files are working. So I wonder if this points more towards the DEBUG configuration issue instead of a static file configuration issue.
Since it’s not a good idea to have DEBUG set to True in production, I am trying to get Django to serve my web pages successfully with DEBUG set to False before I begin adding any of my production blog content.
Quite a number of other Django users have encountered the same issue I have. It’s all over Stack Overflow. Here is someone who has a highly upvoted question who has basically the exact same issue I have:
I’ve tried substituting different static file variable configurations based on the answers recommended in that thread which hasn’t resolved my issue. I feel like I have tried everything. I am all out of ideas.
Based on my configuration files shared below, what might you people recommend I try next?
Here is the DEBUG declaration in my settings.py:
DEBUG = config('DEBUG', default=True, cast=bool)
(At the top of the settings.py file I have imported the config
function from the decouple
module.)
Inside the MIDDLEWARE
variable, I have specified:
"whitenoise.middleware.WhiteNoiseMiddleware",
Here is the section in my settings.py where I declare my static files configuration variables:
STATIC_URL = '/staticfiles/'
STATICFILES_DIRS = [ os.path.join(BASE_DIR,'static') ]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# I got the below from:
# https://stackoverflow.com/questions/53859972/django-whitenoise-500-server-error-in-non-debug-mode
STATICFILES_STORAGE = "whitenoise.storage.CompressedStaticFilesStorage"
The above is what is currently deployed to both staging and prod pipelines. As I mentioned earlier, this configuration works with DEBUG set to True but not when set to False. Based on my rummaging around Stack Overflow, here are some additional variable declarations that I experimented with (but now currently have them commented out):
'''
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = 'static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
]
STATIC_ROOT = BASE_DIR / "hypno_juicer/static"
'''
'''
STATIC_URL = "/static/"
STATICFILES_DIRS = [str(BASE_DIR.joinpath("hypno_juicer/static"))]
STATIC_ROOT = str(BASE_DIR.joinpath("static"))
'''
'''
Commented out this (Originally from John Elder):
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
Accoding to: https://stackoverflow.com/a/66661021/6095646
Trying this instead :
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
'''