1.首先setttings.py里面設置
STATIC_ROOT = os.path.join(BASE_DIR , "mystatic/static") ## 新增行 DEBUG = False ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
2.執(zhí)行python manage.py collectstatic收集靜態(tài)文件, 把各個模塊下的static下的文件拷貝到STATIC_ROOT指定的目錄下
3.在urls.py里面添加路由映射
from django.urls import path,include,re_path
from home import index
from django.conf import settings
import os
urlpatterns = [
#path('admin/', admin.site.urls),
path("",index.index),
path('test/', include("test.urls")),
path('yt/', include('zz.urls')),
path('ueditor/',include('DjangoUeditor.urls'))
]
if not settings.DEBUG:
from django.views import static
urlpatterns += [re_path(r'^static/(?P<path>.*)$', static.serve,
{'document_root': settings.STATIC_ROOT}, name='static')]
urlpatterns += [re_path(r'^media/(?P<path>.*)$', static.serve,
{'document_root': settings.MEDIA_ROOT}, name='media')]
if settings.DEBUG:
from django.conf.urls.static import static
media_root = os.path.join(settings.BASE_DIR,settings.MEDIA_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root = media_root)
#映射 /media 到statics/media目錄