gpt4 book ai didi

python - Heroku Django 应用程序未加载静态文件(404 未找到)

转载 作者:行者123 更新时间:2023-11-28 20:14:59 24 4
gpt4 key购买 nike

我一直在尝试将 django 应用程序部署到 heroku 上。但是,它无法获取静态文件。我在 heroku 上运行 collecstatic 并且在包含正确文件的应用程序的根目录中有一个 static 文件夹:~/static/rest_framework/css/bootstrap.min.css

设置.py:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

curl :

curl 'https://xxx.herokuapp.com/static/rest_framework/css/bootstrap.min.css' \
-XGET \
-H 'Referer: https://xxx.herokuapp.com/users/login' \
-H 'Accept: text/css,*/*;q=0.1' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0.2 Safari/604.4.7'

最佳答案

我花了几个小时才弄明白这一点。 @VipinMohan 解决方案适用于 whitenoise<4。然而,在 4+ 版本中,WhiteNoise 删除了一些在之前的主要版本中被弃用的选项。作为记录,我使用的是 Django 2.1。

来自 the docs :

The WhiteNoise middleware should be placed directly after the Django SecurityMiddleware (if you are using it) and before all other middleware.

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
# the next line of code is the one that solved my problems
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware'
]

请注意所提供链接部分中的注释。

You might find other third-party middleware that suggests it should be given highest priority at the top of the middleware list. Unless you understand exactly what is happening you should ignore this advice and always place WhiteNoiseMiddleware above other middleware.

关于python - Heroku Django 应用程序未加载静态文件(404 未找到),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48128419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com