- settings.py
DEBUG = False STATIC_ROOT = os.path.join(BASE_DIR, 'static')
- urls.py
# 新增导入模块 from django.conf import settings from django.views import static from django.urls import re_path # 文件新增代码 if not settings.DEBUG: urlpatterns += [ re_path(r'^static/(?P<path>.*)$', static.serve, {'document_root': settings.STATIC_ROOT}), ]
- execute command
python manage.py collectstatic
Serving the site and your static files from the same server
If you want to serve your static files from the same server that’s already serving your site, the process may look something like:
- Push your code up to the deployment server.
- On the server, run
collectstatic
to copy all the static files intoSTATIC_ROOT
.- Configure your web server to serve the files in
STATIC_ROOT
under the URLSTATIC_URL
.You’ll probably want to automate this process, especially if you’ve got multiple web servers.
网友评论