Django collectstatic does not update modified files on S3

Asked

Viewed 103 times

1

2 answers

2


I created a Settings file to run collectstatic locally to synchronize with S3. In this file I set:

TIME_ZONE = 'UTC'

Worked perfectly.

I’m using S3 in São Paulo, theoretically the S3 Timezone is the same as my site and should work properly. But it didn’t work. I think the Timezone that shows on the S3 console is different from the real file Timezone.

1

Okay, You even answered your question (before I see it, Hehe!), but I can’t help but add my answer, even to document it as well. After having several problems with Static files, on several servers (each in a different way), I found a solution, which, to me, seems magical.It’s called Whitenoise, below as the magic is done:

1) Install the Whitenoise:

pip install whitenoise

2) Edit your Settings.py and add Whitenoise to the list MIDDLEWARE_CLASSES above all others, from Securitymiddleware to Django:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
     ....
]

3) For archive and cache support, add to settings.py:

 STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

Below is a summary (of a real case) of what is important to have in the file settings.py, in addition to that mentioned in item 2.

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
....

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)

# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

Whitenoise is called "Radically simplified Static file serving for Python web apps", meaning it is not unique to Django, but as Django is python, so... The documentation has a topic dedicated to Django, including instructions for Amazon Cloudfront.

Project website.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.