Django的collectstatic有什么意义? [英] What's the point of Django's collectstatic?

查看:362
本文介绍了Django的collectstatic有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但这只是没有点击我的脑袋.

This is probably a stupid question, but it's just not clicking in my head.

在Django中,惯例是将所有特定于您应用的静态文件(即CSS,JS)放入名为 static 的文件夹中.所以结构看起来像这样:

In Django, the convention is to put all of your static files (i.e css, js) specific to your app into a folder called static. So the structure would look like this:

mysite/
    manage.py
    mysite/ --> (settings.py, etc)
    myapp/ --> (models.py, views.py, etc)
        static/

mysite/settings.py中,我有:

STATIC_ROOT = 'staticfiles'

因此,当我运行命令时:

So when I run the command:

python manage.py collectstatic   

它将在根级别创建一个名为staticfiles的文件夹(与myapp/相同的目录)

It creates a folder called staticfiles at the root level (so same directory as myapp/)

这有什么意义?它不只是创建我所有静态文件的副本吗?

What's the point of this? Isn't it just creating a copy of all my static files?

推荐答案

将多个应用程序中的静态文件收集到单个路径中

好吧,一个Django 项目可能会使用多个 apps ,所以虽然那里只有一个myapp,但实际上可能是myapp1myapp2

Collect static files from multiple apps into a single path

Well, a single Django project may use several apps, so while there you only have one myapp, it may actually be myapp1, myapp2, etc

通过将它们从单个应用程序内部复制到单个文件夹中,您可以将前端Web服务器(例如nginx)指向该单个文件夹STATIC_ROOT并从单个位置提供静态文件,而不是将Web服务器配置为从多个路径提供静态文件.

By copying them from inside the individual apps into a single folder, you can point your frontend web server (e.g. nginx) to that single folder STATIC_ROOT and serve static files from a single location, rather than configure your web server to serve static files from multiple paths.

有关将MD5哈希附加到文件名以进行版本控制的说明:这不是collectstatic的默认行为的一部分,因为settings.STATICFILES_STORAGE的默认行为是StaticFilesStorage(不会这样做)

A note about the MD5 hash being appended to the filename for versioning: It's not part of the default behavior of collectstatic, as settings.STATICFILES_STORAGE defaults to StaticFilesStorage (which doesn't do that)

MD5哈希将在例如如果您将其设置为使用ManifestStaticFilesStorage,则该广告会宣传该行为.

The MD5 hash will kick in e.g. if you set it to use ManifestStaticFilesStorage, which ads that behavior.

此存储的目的是继续提供旧文件,以防万一 某些页面仍引用这些文件,例如因为它们被缓存 您或第三方代理服务器.此外,如果 您想将远期Expires标头应用于已部署的文件 加快后续页面访问的加载时间.

The purpose of this storage is to keep serving the old files in case some pages still refer to those files, e.g. because they are cached by you or a 3rd party proxy server. Additionally, it’s very helpful if you want to apply far future Expires headers to the deployed files to speed up the load time for subsequent page visits.

这篇关于Django的collectstatic有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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