1. settings.py代码
STATIC_URL = '/static/'
# 配置静态文件物理存放目录
# STATICFILES_DIRS = [
# os.path.join(BASE_DIR, "static")
# ]
# 当debug=False时,需要配置静态文件访问的Url
STATIC_ROOT = os.path.join(BASE_DIR, "static")
注意:一旦设置STATIC_ROOT变量,就需要注释掉STATICFILES_DIRS变量
2. 根urls.py代码
from django.urls import re_path
from django.views.static import serve
from django.conf import settings
urlpatterns = [
# 配置debug=False时,静态文件访问的url
re_path("^static/(?P<path>.*)/$", serve, {"document_root": settings.STATIC_ROOT}),
]