Heroku不加载静态文件,Django



部署在Heroku上的项目不加载静态文件。

日志:method=GET path="/static/vendor/bootstrap/js/bootstrap.bundle.min.js"状态= 404

我的设置是(github上的settings.py):

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATICFILES_DIRS = [ BASE_DIR / 'static' ]
如果我修改STATIC_ROOT ,将加载静态文件。
STATIC_ROOT = BASE_DIR / 'static'

但是在本地机器上失败了。

我遵循了Heroku和WhiteNoise的指导方针,几乎没有什么帮助。https://devcenter.heroku.com/articles/django-assetshttp://whitenoise.evans.io/en/stable/django.html

我如何调试或调查问题?

日志:

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

解决方案:经过长时间的调查,我发现在Procfile中运行collecstatic有助于

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

Stackoverflow解决方案

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

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

错误:

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不是有效的文件夹。

在Heroku上部署应用程序时,Heroku将所有文件放在/app目录下。因此,static文件夹将在/app/static中。

试着改成:

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

(虽然我不能完全告诉你为什么以前没有工作,因为我希望Path(__file__).resolve()是/app/giftme/settings.py
.parent将因此是/app/giftme
.parent.parent将是'/app

)如果这解决了Not Found错误,但弹出一个新的错误,请在一个新的问题。