1. 处理静态文件(解决后台样式丢失)
当 DEBUG = False 时,Django 不再自动处理静态文件(CSS、JS 等),需手动配置:
步骤:
- 
配置 STATIC_ROOT和STATIC_URL在 settings.py中确保以下配置:pythonSTATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') # 静态文件收集目录
- 
收集静态文件 运行命令将 Django 内置的静态文件(包括后台样式)收集到 STATIC_ROOT:bashpython manage.py collectstatic
- 
在项目的urls.py文件,创建一下访问这些静态文件的url 
            
            
              python
              
              
            
          
          from django.views import static  
from django.conf import settings  
from django.urls import re_path
urlpatterns = [
re_path(r'^static/(?P<path>.*)$', static.serve, {'document_root': settings.STATIC_ROOT}, name='static'),
]