运维咖啡吧

享受技术带来的乐趣,体验生活给予的感动

Django DEBUG = False导致静态文件加载失败500问题处理

只需三步即可轻松解决:

1.确保settings.py文件有以下三个配置

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

2.在urls中添加如下配置

from django.views import static
from django.conf import settings
from django.urls import path, re_path, include

urlpatterns = [
    re_path('static/(?P<path>.*)$', static.serve, {'document_root': settings.STATIC_ROOT}),
]

3.控制台下执行collectstatic命令收集静态文件

# python manage.py collectstatic