Django local version using remote static files despite local settings

625 views Asked by At

I have managed to successfully deploy my Django app to AWS Elastic Beanstalk (using eb). I have followed the steps laid out here http://www.youtube.com/watch?v=YJoOnKiSYws and here http://grigory.ca/2012/09/getting-started-with-django-on-aws-elastic-beanstalk/ and am using the django-storages (boto) framework to assist with staticfile management to S3.

I have the following settings in my SETTINGS.PY:

STATIC_ROOT = os.path.join(
    os.path.dirname(
        os.path.dirname(
            os.path.abspath(__file__))), 'static')

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_ACCESS_KEY_ID = 'access-key'
AWS_SECRET_ACCESS_KEY = 'secret-key'
AWS_STORAGE_BUCKET_NAME = 'bucket-name'
STATIC_URL = '/static/'
STATICFILES_DIRS = ()
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
.
.
.
try:
 from local_settings import *
except ImportError, e:
 pass

and my LOCAL_SETTINGS.PY has the following:

STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
  ('js', '/blah/blah/static/js'),
  ('css', '/blah/blah/static/css'),
  ('images', '/blah/blah/static/images'),
)

In my templates I use:

{% load staticfiles %}
<link href="{% static "css/styles.css" %}" rel="stylesheet">

The issue is that when I run the app locally it references my static files in S3 and not in my local directories. What settings do I need to change so that when I run the app locally it uses local static files and that when it runs on AWS it uses S3 files?

2

There are 2 answers

0
montiniz On

can you post your urls.py btw you have to have debug=True to serve static locally This might help you to configure your settings and local_settings

0
Patrick Robertson On

The issue is that you have to reset the default storage backends in your local_settings.py file; the settings.py file is processed first, and when you get to local_settings.py, Django is still using the defaults.

An alternative strategy (and perhaps a preferable one) is to test the environment in settings.py to determine if you are local or remote (i.e. development or production) and then decide which settings to use.

edit: So, as an example, the AWS Django guides tell you to do something like this:

if 'RDS_DB_NAME' in os.environ:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': os.environ['RDS_DB_NAME'],
            'USER': os.environ['RDS_USERNAME'],
            'PASSWORD': os.environ['RDS_PASSWORD'],
            'HOST': os.environ['RDS_HOSTNAME'],
            'PORT': os.environ['RDS_PORT'],
        }
    }

And then, in local_settings.py, you define DATABASES for your local configuration. What I have done in the past is something like this:

if 'RDS_DB_NAME' in os.environ:
    LOCAL_ENVIRONMENT = False
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': os.environ['RDS_DB_NAME'],
            'USER': os.environ['RDS_USERNAME'],
            'PASSWORD': os.environ['RDS_PASSWORD'],
            'HOST': os.environ['RDS_HOSTNAME'],
            'PORT': os.environ['RDS_PORT'],
        }
    }

And then use LOCAL_ENVIRONMENT (or whatever you want to name it) to drive the rest of the configuration.