S3 Static & Media Files for Django (more problems)

Trying to setup S3 for static files for my django project but only in Production mode. In local (development) mode I want to use local files.
From one of Coding For Entrepreneurs django videos I got this python snippet to support a production environment and a local environment.
settings/__init__.py

from .base import *
from .production import *

try:
from .local import *
except:
pass

I followed the steps from the Coding For Entrepreneurs blog. I did steps #4, #5, #6 and I assume step #7 means to put the

from <your-project>.aws.conf import *

Into the settings/production.py?
And to leave it out of the settings/local.py?
Using the above settings/__init__.py on my local (development) environment the {% static %} template tag is replaced by https://s3.us-east-2.amazonaws.com/ S3 bucket url
If I comment out the from .production import * from settings/init.py

from .base import *
#from .production import *

try:
from .local import *
except:
pass


I get the local.py STATIC_URL='/static/' from my settings/local.py
It's like the settings/production.py and/or <project>.aws.conf are setting some variables that are not unset or used in the local.py configuration.
What I think are relevant snippets from my settings/production.py

from arch.aws.conf import *
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'storages',

'items',
'searches',
'taggit',
]

STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
STATIC_ROOT = os.path.join(BASE_DIR, "live-static", "static-root")
MEDIA_ROOT = os.path.join(BASE_DIR, "live-static-files", "media-root")

CORS_REPLACE_HTTPS_REFERER = True
HOST_SCHEME = "https://"
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_SECONDS = 1000000
SECURE_FRAME_DENY = True
And the aws/conf.py

Copyimport datetime

AWS_ACCESS_KEY_ID = ""
AWS_SECRET_ACCESS_KEY = ""
AWS_FILE_EXPIRE = 200
AWS_PRELOAD_METADATA = True
AWS_QUERYSTRING_AUTH = True

DEFAULT_FILE_STORAGE = 'arch.aws.utils.MediaRootS3BotoStorage'
STATICFILES_STORAGE = 'arch.aws.utils.StaticRootS3BotoStorage'

AWS_STORAGE_BUCKET_NAME = 'django.archdb'
AWS_DEFAULT_ACL = None
AWS_QUERYSTRING_AUTH = False
AWS_S3_REGION_NAME = 'us-east-2'

#S3_URL = '//%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
#MEDIA_URL = '//%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
#
#MEDIA_ROOT = MEDIA_URL + 'media/'
#STATIC_URL = S3_URL + 'static/'
#
#ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'

two_months = datetime.timedelta(days=61)
date_two_months_later = datetime.date.today() + two_months
expires = date_two_months_later.strftime("%A, %d %B %Y 20:00:00 GMT")

AWS_S3_OBJECT_PARAMETERS = {
'Expires': expires,
'CacheControl': 'max-age=%d' % (int(two_months.total_seconds()), ),
}


I had to comment out the S3_URL and STATIC_URL to get the S3 stuff to work in production (Heroku) but I believe all the necessary variables are overridden in the local.py so I’m thinking the django-storages or boto3 apps are actually getting installed in the production.py INSTALLED_APPS and manipulating the STATIC_URL even when the local.py has overridden the INSTALL_APPS.
UPDATE #1
If I comment out the INSTALLED_APPS in the settings/production.py and uncomment the production configuration in settings/init.py

from .base import *
from .production import *

try:
    from .local import *
except:
    pass


Then on my local (development) computer's the STATIC_URL is correct (not the S3 bucket) so some evidence that the problem is in the INSTALLED_APPS and probably the
django-storages django app.
UPDATE #2
I commented out just the django-storages from INSTALL_APPS in the settings/production.py and the local.py continues to work. I really think django-storages is messing with things.

UPDATE #3

I removed the __pycache__ directories and things are still broken

UPDATE #4

I think I found the problem. My settings/production.py imports the settings/aws/conf.py and settings/aws/conf.py changes the DEFAULT_FILE_STORAGE and the STATICFILES_STORAGE to the S3Boto3Storage.
Now I import the settings/local.py and because settings/local.py doesn't reset the DEFAULT_FILE_STORAGE and the STATICFILES_STORAGE they are still set to S3Boto3Storage.
In settings/local.py I reset the variables:

STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
DEFAULT_FILE_STORAGE = 'django.core.files.storage.FileSystemStorage'

This fixes my problem.

Comments

Popular posts from this blog

How to use the Inbox Repair Tool to recover messages in Outlook 2000 that is installed with Internet Mail Only option

ReadyNAS default login and password

Repair corrupt Outlook PST files with two Microsoft utilities