django DEBUG=False 加载静态资源

  • 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.*)$', 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 into STATIC_ROOT.
  • Configure your web server to serve the files in STATIC_ROOT under the URL STATIC_URL.

You’ll probably want to automate this process, especially if you’ve got multiple web servers.

你可能感兴趣的:(django DEBUG=False 加载静态资源)