Reputation: 1296
How do I get heroku to serve my static files on my django project?
Ok, on a push to heroku, I get this message rlated to staticfiles:
$ python manage.py collectstatic --noinput
remote: 130 static files copied to '/tmp/build_48c64d55/staticfiles', 410 post-processed.
But when I go to my site, clear static files not being recognized and Network tab shows me:
GET https://pure-everglades-09529.herokuapp.com/static/style.css 404 Not Found
This is the content of my html referencing the css I cannot find:
<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="{% static 'style.css' %}">
<title>{% block title %}My amazing site{% endblock %}</title>
</head>
Here is the bottom of my settings.py which is configured just like everywhere says it should be like:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = [
os.path.join(PROJECT_ROOT, 'static'),
]
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
# Activate Django-Heroku.
django_heroku.settings(locals())
Here is higher up in my settings, I have added whitenoise dependencies as recommended everywhere:
INSTALLED_APPS = [
'myapp.apps.MyappConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
# Disable Django's own staticfiles handling in favour of WhiteNoise, for
# greater consistency between gunicorn and `./manage.py runserver`. See:
# http://whitenoise.evans.io/en/stable/django.html#using-whitenoise-in-development
'whitenoise.runserver_nostatic',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
...
]
Lastly, here are my urls, where I added the static urls but only for my local env:
urlpatterns = [
path('', include('myapp.urls')),
path('admin/', admin.site.urls),
]
if 'Owner' in settings.BASE_DIR:
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=setting
What am I doing wrong? Is it because I have DEBUG=True in settings? I set that to False as check but got a 500 error
Upvotes: 1
Views: 314
Reputation: 1296
Apparently
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = [
os.path.join(PROJECT_ROOT, 'static'),
]
needs to be
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
staticfiles still created in correct location, probably because of VARIABLES getting overwritten by
django_heroku.settings(locals())
Upvotes: 1