为什么使用Django的收藏,而不是直接从静态目录提供文件? [英] Why use Django's collectstatic instead of just serving the files directly from your static directory?

查看:123
本文介绍了为什么使用Django的收藏,而不是直接从静态目录提供文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Django文档:


部署django.contrib.staticfiles提供了一个方便的
管理命令,用于收集静态文件一个目录,所以
你可以轻松地为他们服务。

Deployment django.contrib.staticfiles provides a convenience management command for gathering static files in a single directory so you can serve them easily.

将STATIC_ROOT设置设置为要从中
提供这些文件的目录,例如:

Set the STATIC_ROOT setting to the directory from which you’d like to serve these files, for example:

STATIC_ROOT = "/var/www/example.com/static/" 

运行collectstatic管理命令:

Run the collectstatic management command:

$ python manage.py collectstatic

这将从
静态文件夹中复制所有文件进入STATIC_ROOT目录。

This will copy all files from your static folders into the STATIC_ROOT directory.

使用您选择的Web服务器来提供文件。部署静态
文件涵盖了静态文件的一些常见部署策略。

Use a web server of your choice to serve the files. Deploying static files covers some common deployment strategies for static files.

复制文件的目的是什么,为什么不从他们在应用程序内的目录中提供他们?

What's the purpose of copying the files, why not just serve them from the directory they live in within the app?

推荐答案

为什么不只是为你的 / code>目录?您可以使用多个应用程序,并且您的一些应用程序可能不受您的控制。在 staticfiles 应用程序存在之前,您必须手动将所有应用程序的静态文件复制到公用目录,将其上传到CDN,或将其符号链接到文档根目录您的网络服务器。

Why not just serve your static directory? You might use more than one app, and some of your apps may not be under your control. Before the staticfiles app existed, you then had to either manually copy the static files for all apps to a common directory, upload them to your CDN, or symlink them to the document root of your web server.

staticfiles应用程序建立了一个约定:在静态目录下为每个应用程序放置静态文件让Django为你做的工作。

The staticfiles app established a convention: put static files for each app under a static directory and let Django do the work for you.

这篇关于为什么使用Django的收藏,而不是直接从静态目录提供文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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