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<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 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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。