Django設置DEBUG=False後靜態文件沒法加載

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'),
]
相關文章
相關標籤/搜索