Reputation: 4161
I am getting errors with a Django app on Heroku when referencing static files (e.g. CSS). The files are found when running the site on my local Linux machine, but not when it is deployed on Heroku. I have looked at the Django documentationrelating to static files, but I do not understand what I am doing wrong.
django.contrib.staticfiles
is included in my INSTALLED_APPS
.
In my_site/settings.py
I have STATIC_URL = '/static/'
.
In my HTML template I have
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'reco/style.css' %}">
The result is that the CSS file is found when running locally, but not on Heroku. How can I fix this? I want the simplest possible solution - this is a very small site.
# Log message when running the app on the local machine: file is found and has not changed (correct)
[13/May/2020 15:02:52] "GET /static/reco/style.css HTTP/1.1" 304 0
# Log message when running the app on Heroku: not found. Why?
2020-05-13T20:09:20.327218+00:00 app[web.1]: Not Found: /static/reco/style.css
Update: Following suggestions from comments, I have configured the site as advised in the Heroku Django documentation
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'reco', 'static'),
)
Now when I deploy to Heroku I see that collectstatic
is collecting my static files - but Heroku is stil not finding them at runtime.
Upvotes: 0
Views: 1623
Reputation: 1291
As per the Dev Center of Heroku...
You need to install whitenoise to be able to serve static files on production mode.
Follow the installation instructions over here.
Upvotes: 2