Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use Django's collectstatic instead of just serving the files directly from your static directory?

Tags:

django

From the Django Docs:

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

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/" 

Run the collectstatic management command:

$ python manage.py collectstatic

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

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?

like image 244
Ryan Detzel Avatar asked Jan 10 '14 12:01

Ryan Detzel


People also ask

Why do we need Collectstatic?

collectstatic. Collects the static files into STATIC_ROOT . Duplicate file names are by default resolved in a similar way to how template resolution works: the file that is first found in one of the specified locations will be used. If you're confused, the findstatic command can help show you which files are found.

What does manage py Collectstatic do?

Y manage.py collectstatic. This command (don't forget to replace "X.Y" with the version of Python your website uses) collects up all your static files from each of your app folders (including the static files for the admin app) and from any other folders you specify in settings.py, and copies them into STATIC_ROOT .

What does Collectstatic do in Django?

Django also provides a mechanism for collecting static files into one place so that they can be served easily. Using the collectstatic command, Django looks for all static files in your apps and collects them wherever you told it to, i.e. the STATIC_ROOT .


1 Answers

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.

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

like image 190
sk1p Avatar answered Nov 15 '22 17:11

sk1p