Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

python - What's the point of Django's collectstatic?

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

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/

In mysite/settings.py I have:

STATIC_ROOT = 'staticfiles'

So when I run the command:

python manage.py collectstatic   

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

Persistent URLs with ManifestStaticFilesStorage

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)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...