【发布时间】:2022-01-07 20:36:39
【问题描述】:
所以这是 Django 用户的一个常见错误,即Debug=False 时没有在生产中提供静态文件。我已经尝试了很多方法来克服这个问题,但仍然无法找到正确的解决方案。下面是我的settings.py
...
DEBUG = False
MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware',
...
]
...
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
os.path.join(BASE_DIR, 'main/static'),
os.path.join(BASE_DIR, 'member/static'),
os.path.join(BASE_DIR, 'register/static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
我不明白我做错了什么。
*可能是潜在原因的一件事是,自从最初部署到 heroku 服务器以来,我已经设置了 DISABLE_COLLECTSTATIC=1。
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Building on the Heroku-20 stack
remote: -----> Using buildpack: heroku/python
remote: -----> Python app detected
remote: -----> Using Python version specified in runtime.txt
remote: -----> No change in requirements detected, installing from cache
remote: -----> Using cached install of python-3.7.12
remote: -----> Installing pip 21.3.1, setuptools 57.5.0 and wheel 0.37.0
remote: -----> Installing SQLite3
remote: -----> Installing requirements with pip
remote: -----> Skipping Django collectstatic since the env var DISABLE_COLLECTSTATIC is set.
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing...
remote: Done: 102.1M
remote: -----> Launching...
remote: Released v153
【问题讨论】:
-
你
collect static了吗? -
@Jonas 每次更改静态文件时我都必须
collect static吗?我做过一次,从那时起就没有添加任何静态文件。 (不过对现有的进行了一些更改。) -
是的,如果您添加/删除/更改静态文件,您必须重新收集它们
-
@Jonas 我明白了。所以我必须
python manage.py collectstatic然后部署到heroku服务器,对吧?