Django - 推荐的方式来提供静态文件,而不必在部署时更改太多代码?

时间:2022-04-07 20:30:07

This link: https://docs.djangoproject.com/en/1.8/howto/static-files/ starts off by explaining a "grossly inefficient and probably insecure, unsuitable for production" way of serving files. It also explains how to use

这个链接:https://docs.djangoproject.com/en/1.8/howto/static-files/首先解释了“非常低效,可能不安全,不适合生产”的文件服务方式。它还解释了如何使用

 django.contrib.staticfiles.views.serve()

which is "not suitable for production use!". I don't want to have to code an entire Django project, finish it and then end up having to change a lot of code right before deploying because the current code is "grossly inefficient and probably insecure, unsuitable for production".

这是“不适合生产使用!”。我不想编写整个Django项目的代码,完成它然后最终必须在部署之前更改大量代码,因为当前代码“非常低效且可能不安全,不适合生产”。

With that said, what's the best approach (the approach which doesn't require a lot of extra work just to move my app from development to production) to take when serving static files? I'm asking because I've never been through the process of deploying a Django app before and when deploying, I don't want to end up saying to myself "wow, I should have done it that way rather than this way".

话虽如此,在提供静态文件时,最好的方法是什么(这种方法不需要额外的工作,只需将我的应用从开发转移到生产)?我问,因为我从未经历过在部署之前和部署时部署Django应用程序的过程,我不想最终对自己说“哇,我应该这样做而不是这样”。

1 个解决方案

#1


1  

You don't change any code, because it's not your code that serves static files. You need to configure your web server to do it; which is fine, because you're configuring your web server anyway for deployment.

您不会更改任何代码,因为它不是您的代码提供静态文件。您需要配置Web服务器来执行此操作;这很好,因为您无论如何都要配置您的Web服务器以进行部署。

The whole point of the staticfiles app in Django is just that, that it manages files for you in development and puts them in a single place for deployment so you can point your web server at them.

Django中的staticfiles应用程序的重点就在于,它在开发中为您管理文件并将它们放在一个部署位置,以便您可以将Web服务器指向它们。

#1


1  

You don't change any code, because it's not your code that serves static files. You need to configure your web server to do it; which is fine, because you're configuring your web server anyway for deployment.

您不会更改任何代码,因为它不是您的代码提供静态文件。您需要配置Web服务器来执行此操作;这很好,因为您无论如何都要配置您的Web服务器以进行部署。

The whole point of the staticfiles app in Django is just that, that it manages files for you in development and puts them in a single place for deployment so you can point your web server at them.

Django中的staticfiles应用程序的重点就在于,它在开发中为您管理文件并将它们放在一个部署位置,以便您可以将Web服务器指向它们。