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 @ 2020-09-20 09:42  sunny开始学坏  阅读(1171)  评论(0编辑  收藏  举报