解决使用admin功能时, 一部分 css和js文件无法加载
原因是由settings.py修改debug = true 为 debug =false 引起的,
1、在settings.py添加如下
STATIC_URL = '/static/'
#STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
2、在项目目录下,新建文件夹 static,运行下面命令把相关文件copy到static这个文件夹
python manage.py collectstatic
3、在项目下的urls.py中,urlpatterns下面添加:
from django.contrib import admin
from django.urls import path,include,re_path
from django.conf import settings
from django.conf.urls.static import serve,static
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^static/(?P<path>.*)$', serve, {'document_root': settings.STATIC_ROOT}),
]