django 踩坑

project下settings.py修改 DEBUG = False 导致默认的后台样式丢失
在settings.py里新增STATIC_ROOT = os.path.dirname(__file__),或者直接将STATIC_ROOT=’settings.py所在目录的绝对地址’
根目录执行

shell> python manage.py collectstatic

然后配置静态资源url,编辑project里的urls.py,在urlpatterns里加入

from django.contrib import admin
from django.urls import path, include
from django.conf.urls import url
from django.views import static
from django.conf import settings

urlpatterns = [
    path('admin/', admin.site.urls),
    path('polls/', include('polls.urls')),
    path('blog/', include('blog.urls')),
    # 资源路由匹配
    url(r'^static/(?P<path>.*)$', static.serve, {'document_root': settings.STATIC_ROOT}, name='static'),
]

Leave Comment

电子邮件地址不会被公开。 必填项已用*标注