django设置debug=false时静态文件丢失

1.设置settings.py

复制代码
DEBUG = False
ALLOWED_HOSTS = ["*"]

...


STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
复制代码

2.修改项目的urls.py

复制代码
from django.contrib import admin
from django.urls import path
from django.urls import include, path,re_path
from django.views.static import serve

from django.conf import settings
from django.conf.urls.static import static


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

3.执行django转移静态文件命令

python manage.py collectstatic

4.问题解决

 

posted @ 2022-02-01 22:53  CrossPython  阅读(912)  评论(0编辑  收藏  举报