首先原因是因为禁止了DEBUGDEBUG = False,所以导致找不到静态资源,但是生产环境是必须要关闭DEBUG的,所以我们需要找给Django开设一条路由来解决此问题。
DEBUG = False
settings配置如下所示:
STATIC_URL = '/static/' if DEBUG: STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static"), ] else: STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# 把英文改为中文 LANGUAGE_CODE = 'zh-hans' # 把国际时区改为中国时区 TIME_ZONE = 'Asia/Shanghai'
项目主urls配置如下所示:
from django.contrib import admin from django.urls import path, re_path, include from django.views.static import serve from django.conf import settings urlpatterns = [ path('admin/', admin.site.urls), re_path('^static/(?P<path>.*)', serve, {'document_root': settings.STATIC_ROOT}), ]
python manage.py collectstatic
然后重启你的托管服务如gunicorn,没有的话就直接重启Django就可以了。