0

The project deployed on Heroku does not load static files.

Log: method=GET path="/static/vendor/bootstrap/js/bootstrap.bundle.min.js" status=404

My settings are (settings.py on github):

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',

STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATICFILES_DIRS = [ BASE_DIR / 'static' ]

Static files are loaded if I modify STATIC_ROOT

STATIC_ROOT = BASE_DIR / 'static'

But then it fails on a local machine.

I have followed both Heroku and WhiteNoise guidelines with little help. https://devcenter.heroku.com/articles/django-assets http://whitenoise.evans.io/en/stable/django.html

How could I debug or investigate the issue?

Logs:

2021-02-28T17:19:18.172529+00:00 heroku[web.1]: State changed from crashed to starting
2021-02-28T17:19:23.531435+00:00 heroku[web.1]: Starting process with command `gunicorn giftme.wsgi --log-file -`
2021-02-28T17:19:26.396437+00:00 app[web.1]: [heroku-exec] Starting
2021-02-28T17:19:26.782454+00:00 app[web.1]: [2021-02-28 17:19:26 +0000] [4] [INFO] Starting gunicorn 20.0.4
2021-02-28T17:19:26.783026+00:00 app[web.1]: [2021-02-28 17:19:26 +0000] [4] [INFO] Listening at: http://0.0.0.0:22685 (4)
2021-02-28T17:19:26.783123+00:00 app[web.1]: [2021-02-28 17:19:26 +0000] [4] [INFO] Using worker: sync
2021-02-28T17:19:26.788238+00:00 app[web.1]: [2021-02-28 17:19:26 +0000] [33] [INFO] Booting worker with pid: 33
2021-02-28T17:19:26.843959+00:00 app[web.1]: [2021-02-28 17:19:26 +0000] [34] [INFO] Booting worker with pid: 34
2021-02-28T17:19:27.198738+00:00 heroku[web.1]: State changed from starting to up
2021-02-28T17:19:27.744915+00:00 app[web.1]: /app/.heroku/python/lib/python3.7/site-packages/environ/environ.py:630: UserWarning: /app/giftme/.env doesn't exist - if you're not configuring your environment separately, create one.
2021-02-28T17:19:27.744926+00:00 app[web.1]: "environment separately, create one." % env_file)
2021-02-28T17:19:27.745427+00:00 app[web.1]: /app/.heroku/python/lib/python3.7/site-packages/environ/environ.py:630: UserWarning: /app/giftme/.env doesn't exist - if you're not configuring your environment separately, create one.
2021-02-28T17:19:27.745429+00:00 app[web.1]: "environment separately, create one." % env_file)
2021-02-28T17:19:30.000000+00:00 app[api]: Build succeeded
2021-02-28T17:19:39.368742+00:00 app[web.1]: asyncio      DEBUG    Using selector: EpollSelector
2021-02-28T17:19:39.409679+00:00 app[web.1]: 10.63.145.103 - - [28/Feb/2021:19:19:39 +0200] "GET /wishlist/ HTTP/1.1" 200 3694 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36"
2021-02-28T17:19:39.409851+00:00 heroku[router]: at=info method=GET path="/wishlist/" host=stormy-ocean-26616.herokuapp.com request_id=1b0eacb8-c5fc-47e3-97ca-7d92a5a0a246 fwd="91.245.78.97" dyno=web.1 connect=0ms service=141ms status=200 bytes=3956 protocol=https
2021-02-28T17:19:39.656479+00:00 app[web.1]: django.request WARNING  Not Found: /static/vendor/bootstrap/css/bootstrap.min.css
2021-02-28T17:19:39.657084+00:00 app[web.1]: 10.63.145.103 - - [28/Feb/2021:19:19:39 +0200] "GET /static/vendor/bootstrap/css/bootstrap.min.css HTTP/1.1" 404 179 "https://stormy-ocean-26616.herokuapp.com/wishlist/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36"
2021-02-28T17:19:39.657266+00:00 heroku[router]: at=info method=GET path="/static/vendor/bootstrap/css/bootstrap.min.css" host=stormy-ocean-26616.herokuapp.com request_id=468adfda-f5c6-4deb-a6a3-83a1d7441e59 fwd="91.245.78.97" dyno=web.1 connect=0ms service=23ms status=404 bytes=418 protocol=https
2021-02-28T17:19:39.666996+00:00 app[web.1]: django.request WARNING  Not Found: /static/fa/css/all.min.css
2021-02-28T17:19:39.667486+00:00 app[web.1]: 10.97.137.170 - - [28/Feb/2021:19:19:39 +0200] "GET /static/fa/css/all.min.css HTTP/1.1" 404 179 "https://stormy-ocean-26616.herokuapp.com/wishlist/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36"
2021-02-28T17:19:39.668254+00:00 app[web.1]: django.request WARNING  Not Found: /static/vendor/jquery/jquery.slim.min.js
2021-02-28T17:19:39.668414+00:00 heroku[router]: at=info method=GET path="/static/vendor/jquery/jquery.slim.min.js" host=stormy-ocean-26616.herokuapp.com request_id=8d8a55b2-81cb-457b-b383-0403bc2668f9 fwd="91.245.78.97" dyno=web.1 connect=0ms service=4ms status=404 bytes=418 protocol=https
2021-02-28T17:19:39.668959+00:00 app[web.1]: 10.33.162.24 - - [28/Feb/2021:19:19:39 +0200] "GET /static/vendor/jquery/jquery.slim.min.js HTTP/1.1" 404 179 "https://stormy-ocean-26616.herokuapp.com/wishlist/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36"
2021-02-28T17:19:39.674375+00:00 heroku[router]: at=info method=GET path="/static/fa/css/all.min.css" host=stormy-ocean-26616.herokuapp.com request_id=34a8f989-7977-4bc0-8965-3150c8ce858b fwd="91.245.78.97" dyno=web.1 connect=1ms service=3ms status=404 bytes=418 protocol=https
2021-02-28T17:19:39.823515+00:00 app[web.1]: django.request WARNING  Not Found: /static/vendor/bootstrap/js/bootstrap.bundle.min.js
2021-02-28T17:19:39.824044+00:00 app[web.1]: 10.63.145.103 - - [28/Feb/2021:19:19:39 +0200] "GET /static/vendor/bootstrap/js/bootstrap.bundle.min.js HTTP/1.1" 404 179 "https://stormy-ocean-26616.herokuapp.com/wishlist/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36"
2021-02-28T17:19:39.824622+00:00 heroku[router]: at=info method=GET path="/static/vendor/bootstrap/js/bootstrap.bundle.min.js" host=stormy-ocean-26616.herokuapp.com request_id=b9ab5d50-1303-47b1-b6dd-1e74ca1f48d5 fwd="91.245.78.97" dyno=web.1 connect=0ms service=3ms status=404 bytes=418 protocol=https

SOLUTION: After a long investigation, I have found that running collecstatic in Procfile helps

web: python manage.py collectstatic --no-input; gunicorn giftme.wsgi

Stackoverflow solution

  • you can debug the issue using > heroku logs. did you run> heroku run python manage.py collectstatic. Also did you set dj_database_url – Siva Sankar Feb 28 '21 at 16:53
  • @SivaSankar Yes, I have got both dj-database-url and whitenoise in requirements.txt file. collectstatic finishes with status 0. – Andrii Oshtuk Feb 28 '21 at 17:28

1 Answers1

0

https://github.com/AndriiOshtuk/giftme/blob/daf12e22c06d2a9d090c89397143e75edaa1c3a9/giftme/settings.py#L24:

BASE_DIR = Path(__file__).resolve().parent.parent

Error:

2021-02-28T17:19:39.656479+00:00 app[web.1]: django.request WARNING  Not Found: /static/vendor/bootstrap/css/bootstrap.min.css
2021-02-28T17:19:39.666996+00:00 app[web.1]: django.request WARNING  Not Found: /static/fa/css/all.min.css
2021-02-28T17:19:39.668254+00:00 app[web.1]: django.request WARNING  Not Found: /static/vendor/jquery/jquery.slim.min.js
2021-02-28T17:19:39.823515+00:00 app[web.1]: django.request WARNING  Not Found: /static/vendor/bootstrap/js/bootstrap.bundle.min.js

/static is not a valid folder.

When deploying your app on Heroku Heroku places all your files under the /app directory. The static folder would consequently be in /app/static.

Try change it to:

BASE_DIR = Path(__file__).resolve().parent

(Although I can't quite tell you why the previous didn't work because I'd expect Path(__file__).resolve() to be /app/giftme/settings.py
.parent would consequently be /app/giftme
.parent.parent would be `/app

If this resolved the Not Found error but a new error pops up ask it in a new question.

Tin Nguyen
  • 5,250
  • 1
  • 12
  • 32
  • Thank you for replying. I cannot modify BASE_DIR since it will break all my paths. All paths in settings.py file are relative to BASE_DIR – Andrii Oshtuk Mar 03 '21 at 20:31