网上有很多的解决方案,但是很多都没有完整的说明需要新引入哪些文件。
1. 首先修改App setting.py文件
STATIC_URL = '/static/'
STATIC_ROOT = 'static' ## 新增行
STATICFILES_DIRS = [
os.path.join(BASE_DIR, '/static/'), ##修改地方
]
2. 修改urls.py
from django.views import static ##新增
from django.conf import settings ##新增
from django.conf.urls import url ##新增
urlpatterns = [
path('', include('user.urls')),
## 以下是新增
url(r'^static/(?P<path>.*)$', static.serve,
{'document_root': settings.STATIC_ROOT}, name='static'),
]
到这里再运行应该是可以了,如果还有报STATIC_ROOT错。如果是下面的提示,请修改参见第一点的。
ERRORS: ?:
(staticfiles.E002) The STATICFILES_DIRS setting
should not contain the STATIC_ROOT setting.
System check identified 1 issue (0 silenced)
.
os.path.join(BASE_DIR, ‘/static/’), #多加了/