美文网首页
django DEBUG=False 加载静态资源

django DEBUG=False 加载静态资源

作者: Jedore | 来源:发表于2018-08-30 10:00 被阅读0次
  • 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.

相关文章

网友评论

      本文标题:django DEBUG=False 加载静态资源

      本文链接:https://www.haomeiwen.com/subject/cdqrwftx.html