为什么不在生产环境中使用Django提供静态文件? [英] Why not serving static files with Django in a production environment?

查看:112
本文介绍了为什么不在生产环境中使用Django提供静态文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 settings.py 的以下示例:

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

并被告知:

static()辅助函数适用于开发,但不适用于生产使用.永远不要在Django中提供静态文件生产环境.

The static() helper function is suitable for development but not for production use. Never serve your static files with Django in a production environment.

有人可以解释为什么以及如何更好地使用它吗?

Can anyone explain why and how to use it the better way?

我可以在Apache上使用static()吗?

Can I use static() with Apache?

推荐答案

Django在提供静态文件方面不是非常快速或有效.引用Django文档,此方法效率极低,而且可能不安全,因此不适合生产."最好使用专门为提供静态内容而设计的工具.部署静态文件.

Django is not very fast or efficient for serving static files. To quote the Django docs, "This method is grossly inefficient and probably insecure, so it is unsuitable for production." It is better to use tools that are specifically designed for serving static content. There are extensive instructions for how to setup a static server in the Django documentation on deploying static files.

基本思想是不要在不必要的静态文件服务中涉及Django.让您的生产服务器(根据您的评论,听起来像是apache)直接提供静态文件.这是有关编辑httpd.conf文件以使apache服务于静态文件的说明, https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/#serving-files .django中的static()函数根本不应该涉及.确保在django中使用collectstatic management命令将所有静态文件复制到STATIC_ROOT,以便apache可以找到它们.

The basic idea is to not unnecessarily involve Django in the serving of static files. Let your production server, which from your comment it sounds like is apache, serve the static files directly. Here are instructions for editing your httpd.conf file to get apache to serve the static files https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/#serving-files. The static() function in django should not be involved at all. Make sure to use the collectstatic management command in django to copy all your static files to the STATIC_ROOT so apache can find them.

这篇关于为什么不在生产环境中使用Django提供静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆