Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Only collect changed static files

I'm using amazon s3 to store all of my static files (via django-storages) and it costs a lot more money to do PUTs than it does GETs. When I run manage.py collectstatic, Django does a PUT for every single static file I have. Is there a way to have it check first to see if the file has changed at all, and if it hasn't don't bother with the PUT?

like image 607
Kurtis Nusbaum Avatar asked Feb 26 '12 18:02

Kurtis Nusbaum


People also ask

What does collect static do in Django?

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 is STATIC_URL in Django?

STATIC_URL is simply the prefix or url that is prepended to your static files and is used by the static method in Django templates mostly. For more info, read this. STATIC_ROOT is the directory or location where your static files are deployed when you run collectstatic .

What is Collectstatic Noinput?

So django by default overwrites your modified files on collectstatic command, --noinput flag means it will not ask for your permission. Follow this answer to receive notifications. answered Mar 17, 2021 at 19:28.

What does {% load static %} do in Django?

The {% static %} template tag generates the absolute URL of static files. That's all you need to do for development. Reload http://localhost:8000/polls/ and you should see that the question links are green (Django style!) which means that your stylesheet was properly loaded.


1 Answers

It appears that all you need to do is install python-dateutil:

pip install python-dateutil==1.2

Without this django-storages won't check the dates because of this code:

def modified_time(self, name):
  try:
    from dateutil import parser, tz
  except ImportError:
    raise NotImplementedError()

The modified_time throws an error but django just keeps going because it allows the modified_time method of a storage subclass to be unimplemented. I understand why they do it, because this functionality isn't strictly needed. That said, it'd be nice to have some sort of warning saying why EVERYTHING is being uploaded.

Note that I'm using python-dateutil version 1.2. If you use the most latest version of dateutil, you'll get an error with django-storages (that is django-storages version 1.1.4).

like image 66
Kurtis Nusbaum Avatar answered Sep 30 '22 11:09

Kurtis Nusbaum