From a6eefe40b9af7fd7bafd42faab074797b54576c9 Mon Sep 17 00:00:00 2001 From: Nate Aune Date: Thu, 26 Sep 2013 14:45:46 -0400 Subject: [PATCH] make it possible to serve up static assets using dj-static instead of S3 --- {{cookiecutter.repo_name}}/Procfile | 2 +- .../requirements/production.txt | 3 +- .../config/settings.py | 54 +++++++++++-------- .../{{cookiecutter.repo_name}}/config/wsgi.py | 4 +- 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/{{cookiecutter.repo_name}}/Procfile b/{{cookiecutter.repo_name}}/Procfile index 0a678489..3cdbd490 100644 --- a/{{cookiecutter.repo_name}}/Procfile +++ b/{{cookiecutter.repo_name}}/Procfile @@ -1 +1 @@ -web: python {{cookiecutter.repo_name}}/manage.py run_gunicorn --settings=config.settings -b "0.0.0.0:$PORT" -w 3 \ No newline at end of file +web: gunicorn {{cookiecutter.repo_name}}/config/wsgi.py \ No newline at end of file diff --git a/{{cookiecutter.repo_name}}/requirements/production.txt b/{{cookiecutter.repo_name}}/requirements/production.txt index dcaeea79..72151eb2 100644 --- a/{{cookiecutter.repo_name}}/requirements/production.txt +++ b/{{cookiecutter.repo_name}}/requirements/production.txt @@ -5,4 +5,5 @@ gunicorn==0.17.4 django-storages==1.1.4 gevent==0.13.8 -boto==2.9.5 \ No newline at end of file +boto==2.9.5 +dj-static==0.0.5 \ No newline at end of file diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/config/settings.py b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/config/settings.py index 78722575..b77d776c 100644 --- a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/config/settings.py +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/config/settings.py @@ -326,32 +326,42 @@ class Production(Common): INSTALLED_APPS += ("gunicorn", ) - ########## STORAGE CONFIGURATION - # See: http://django-storages.readthedocs.org/en/latest/index.html - INSTALLED_APPS += ( - 'storages', - ) + try: # serve static assets using S3 + ########## STORAGE CONFIGURATION + # See: http://django-storages.readthedocs.org/en/latest/index.html + INSTALLED_APPS += ( + 'storages', + ) - # See: http://django-storages.readthedocs.org/en/latest/backends/amazon-S3.html#settings - STATICFILES_STORAGE = DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage' + # See: http://django-storages.readthedocs.org/en/latest/backends/amazon-S3.html#settings + STATICFILES_STORAGE = DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage' - # See: http://django-storages.readthedocs.org/en/latest/backends/amazon-S3.html#settings - AWS_ACCESS_KEY_ID = values.SecretValue() - AWS_SECRET_ACCESS_KEY = values.SecretValue() - AWS_STORAGE_BUCKET_NAME = values.SecretValue() - AWS_AUTO_CREATE_BUCKET = True - AWS_QUERYSTRING_AUTH = False + # See: http://django-storages.readthedocs.org/en/latest/backends/amazon-S3.html#settings + AWS_ACCESS_KEY_ID = values.SecretValue() + AWS_SECRET_ACCESS_KEY = values.SecretValue() + AWS_STORAGE_BUCKET_NAME = values.SecretValue() + AWS_AUTO_CREATE_BUCKET = True + AWS_QUERYSTRING_AUTH = False - # AWS cache settings, don't change unless you know what you're doing: - AWS_EXPIREY = 60 * 60 * 24 * 7 - AWS_HEADERS = { - 'Cache-Control': 'max-age=%d, s-maxage=%d, must-revalidate' % (AWS_EXPIREY, - AWS_EXPIREY) - } + # AWS cache settings, don't change unless you know what you're doing: + AWS_EXPIREY = 60 * 60 * 24 * 7 + AWS_HEADERS = { + 'Cache-Control': 'max-age=%d, s-maxage=%d, must-revalidate' % (AWS_EXPIREY, + AWS_EXPIREY) + } + + # See: https://docs.djangoproject.com/en/dev/ref/settings/#static-url + STATIC_URL = 'https://s3.amazonaws.com/%s/' % AWS_STORAGE_BUCKET_NAME + ########## END STORAGE CONFIGURATION + except: # serve static assets using wsgi + PROJECT_PATH = os.path.dirname(os.path.abspath(__file__)) + STATIC_ROOT = 'staticfiles' + STATIC_URL = '/static/' + + STATICFILES_DIRS = ( + join(PROJECT_PATH, 'static'), + ) - # See: https://docs.djangoproject.com/en/dev/ref/settings/#static-url - STATIC_URL = 'https://s3.amazonaws.com/%s/' % AWS_STORAGE_BUCKET_NAME - ########## END STORAGE CONFIGURATION ########## EMAIL DEFAULT_FROM_EMAIL = values.Value( diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/config/wsgi.py b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/config/wsgi.py index 0225ba19..e918d461 100644 --- a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/config/wsgi.py +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/config/wsgi.py @@ -26,7 +26,9 @@ os.environ.setdefault("DJANGO_CONFIGURATION", "Local") # file. This includes Django's development server, if the WSGI_APPLICATION # setting points here. from configurations.wsgi import get_wsgi_application -application = get_wsgi_application() +from dj_static import Cling + +application = Cling(get_wsgi_application()) # Apply WSGI middleware here. # from helloworld.wsgi import HelloWorldApplication