[英]Issue with django staticfiles amazon S3
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. 我只是遇到了这个问题,我想知道是否能为您提供帮助,我正在使用django 1.7,我的项目在本地运行良好,我在Amazon EC2中进行了部署,使用了nginx,gunicorn等,该项目在EC2的实例,但是当我在production.py中配置设置时,样式,图像会上传到S3中的存储桶中,但不会加载项目在主页上的内容。 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. 请帮助我,在这里我输入了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. 我使用了python manage.py collectfiles和,它会收集信息,但是当我在网络上打开项目时,它不会加载样式。
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. 我已经安装了boto和django存储设备。eveything在服务器上工作正常,问题出在s3中的static上。
in my manage.py and wsgi.py I specify to use de production.py file. 在我的manage.py和wsgi.py中,我指定使用de production.py文件。
I have a folder named "media", where all the files the people upload, will save there. 我有一个名为“ media”的文件夹,人们上传的所有文件都将保存在其中。 But that folder doenst apper in s3. 但是,该文件夹在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.. 我正在使用2个应用程序,一个用于事件,另一个用于用户..它们都存储图像。
Thank you so much. 非常感谢。
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? 另请参阅: 如何使用django-storages和Amazon S3设置Django项目,但为静态文件和媒体文件设置不同的文件夹?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.