0

i just having this issue, and I wonder if you can help me, I'm using django 1.7, my project runs great on local, I make a deploy in EC2 of amazon, with nginx, gunicorn, etc, the project runs well in the instance of EC2, but when I config the settings in the production.py, the styles, images upload to the bucket in S3, but doenst load in the project, in the main page. Im using pillow for the administration of the images and static.. I just dont know why it doesnt work, because the static files are in the bucket. Please help me, here I put the codes of my base.py, production.py. I used python manage.py collectfiles and , it collects, but it doesnt load the styles when I open the project in the web.

BASE.PY

from unipath import Path

BASE_DIR = Path(__file__).ancestor(3)

SECRET_KEY = '<my secret key>'



DJANGO_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    )

LOCAL_APPS = (

    'apps.eventos',
    'apps.users',

    )

THIRD_PARTY_APPS = (

    'social.apps.django_app.default',

    )

INSTALLED_APPS = DJANGO_APPS + LOCAL_APPS + THIRD_PARTY_APPS

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'ueventos.urls'
WSGI_APPLICATION = 'ueventos.wsgi.application'

LANGUAGE_CODE = 'es'
TIME_ZONE = 'UTC'
DATE_FORMAT = "Y-m-d"
USE_I18N = True
USE_L10N = False
USE_TZ = True


AUTH_USER_MODEL = 'users.User'

AUTHENTICATION_BACKENDS = (
        'social.backends.facebook.FacebookAppOAuth2',
        'social.backends.facebook.FacebookOAuth2',
        'social.backends.google.GoogleOpenId',
        'social.backends.google.GoogleOAuth2',
        'social.backends.google.GoogleOAuth',
        'django.contrib.auth.backends.ModelBackend',
    )

SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/'
SOCIAL_AUTH_USER_MODEL = 'users.User'

SOCIAL_AUTH_FACEBOOK_KEY = '776670565804594'
SOCIAL_AUTH_FACEBOOK_SECRET = 'fcc2a240ef71ff88c15fd64d685bf4eb'

SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = '435717486239-b9dpg2pfian4h8dj1vc95jna8tfdeced.apps.googleusercontent.com'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'oE9ZzK_p9zuNxH91gRLS8nH1'

PRODUCTION.PY

from .base import *

DEBUG = False
TEMPLATE_DEBUG = False
ALLOWED_HOSTS = ['*']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': '<mydb>',
        'USER': '<mydbuser>',
        'PASSWORD' : 'mydbuser',
        'HOST': 'localhost',
        'PORT': '5432'

    }
}

INSTALLED_APPS = INSTALLED_APPS + (

    'storages',
    )

AWS_STORAGE_BUCKET_NAME = 'ueventos'
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorages'
AWS_ACCESS_KEY_ID = 'mykey'
AWS_SECRET_ACCESS_KEY = 'mysecretkey'

STATIC_URL = 'https://s3.amazonaws.com/ueventos/'
MEDIA_URL = 'https://s3.amazonaws.com/ueventos/'

MEDIA_ROOT = BASE_DIR.child('media')

I already install boto and django storages.. eveything works fine on the server, the issue is with the static in s3.

in my manage.py and wsgi.py I specify to use de production.py file.

I have a folder named "media", where all the files the people upload, will save there. But that folder doenst apper in s3. I tried uploading some image in my site, but doesnt work anyways.

I'm using 2 apps, one for events and another for user.. both of them storage images..

Thank you so much.

soultaker
  • 15
  • 1
  • 6

1 Answers1

0

Try something like this:

from storages.backends.s3boto import S3BotoStorage

StaticRootS3BotoStorage = lambda: S3BotoStorage(location='static')
MediaRootS3BotoStorage = lambda: S3BotoStorage(location='media')

DEFAULT_FILE_STORAGE = 'config.settings.production.MediaRootS3BotoStorage'
STATICFILES_STORAGE = 'config.settings.production.StaticRootS3BotoStorage'

MEDIA_URL = 'https://s3.amazonaws.com/%s/media/' % AWS_STORAGE_BUCKET_NAME
STATIC_URL = 'https://s3.amazonaws.com/%s/static/' % AWS_STORAGE_BUCKET_NAME

Adjust path accordingly.

Also see:How to set-up a Django project with django-storages and Amazon S3, but with different folders for static files and media files?

Community
  • 1
  • 1
mishbah
  • 5,487
  • 5
  • 25
  • 35