https://docs.djangoproject.com/en/2.1/howto/static-files/
在开发过程中,如果你使用 django.contrib.staticfiles,这将
当 DEBUG 设置为 True 时由 runserver 自动完成(参见
django.contrib.staticfiles.views.serve())。
因此,当您更改 DEBUG=False 以退出开发时,Django runserver 将不再自动提供静态文件。
这是使用http://whitenoise.evans.io/en/stable/django.html的替代方法
用于在部署中提供静态文件的白噪声
1)pip install whitenoise
2) 将'whitenoise' 添加到 installed_apps
3) 将STATIC_ROOT 提供给如下文件夹:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
4) 运行收集STATIC_ROOT目录下的静态文件
python manage.py collectstatic
5)添加STATICFILES_STORAGE(可选)
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
6) 在除'django.middleware.security.SecurityMiddleware' 之外的所有内容之前添加中间件,例如:
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
...
]
7)在settings.py中改为DEBUG=False
更多详情在这里http://whitenoise.evans.io/en/stable/django.html