Miserlou / Zappa

Serverless Python
https://blog.zappa.io/
MIT License
11.88k stars 1.2k forks source link

Media files does not show up in production #1657

Open Abishek05 opened 5 years ago

Abishek05 commented 5 years ago

Context

Steps to Reproduce

  1. Install django cms, django storages.
  2. Modify settings.py and custom_storages.py accordingly.
  3. Deploy

Generated by 'django-admin startproject' using Django 1.11.15.

For more information on this file, see https://docs.djangoproject.com/en/1.11/topics/settings/

For the full list of settings and their values, see https://docs.djangoproject.com/en/1.11/ref/settings/ """

import os

Build paths inside the project like this: os.path.join(BASE_DIR, ...)

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file)))

Quick-start development settings - unsuitable for production

See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

SECURITY WARNING: keep the secret key used in production secret!

SECRETKEY = '2d-yyc@k&d%5&o2p8hje#duncrtmx8)2vig69$k@5_bubigygfc'

SECURITY WARNING: don't run with debug turned on in production!

DEBUG = False

ALLOWED_HOSTS = ['*']

Application definition

ROOT_URLCONF = 'mysite.urls'

Internationalization

https://docs.djangoproject.com/en/1.11/topics/i18n/

LANGUAGE_CODE = 'en'

TIME_ZONE = 'Asia/Calcutta'

USE_I18N = True

USE_L10N = True

USE_TZ = True

Static files (CSS, JavaScript, Images)

https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(DATA_DIR, 'media')

STATIC_ROOT = os.path.join(DATA_DIR, 'static')

STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'mysite', 'static'), ) SITE_ID = 1

TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'mysite', 'templates'),], 'OPTIONS': { 'context_processors': [ 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', 'django.template.context_processors.i18n', 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.template.context_processors.media', 'django.template.context_processors.csrf', 'django.template.context_processors.tz', 'sekizai.context_processors.sekizai', 'django.template.context_processors.static', 'cms.context_processors.cms_settings' ], 'loaders': [ 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', 'django.template.loaders.eggs.Loader' ], }, }, ]

MIDDLEWARE = ( 'cms.middleware.utils.ApphookReloadMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.locale.LocaleMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'cms.middleware.user.CurrentUserMiddleware', 'cms.middleware.page.CurrentPageMiddleware', 'cms.middleware.toolbar.ToolbarMiddleware', 'cms.middleware.language.LanguageCookieMiddleware' )

INSTALLED_APPS = ( 'djangocms_admin_style', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.admin', 'django.contrib.sites', 'django.contrib.sitemaps', 'django.contrib.staticfiles', 'django.contrib.messages', 'cms', 'menus', 'sekizai', 'treebeard', 'djangocms_text_ckeditor', 'filer', 'easy_thumbnails', 'djangocms_column', 'djangocms_file', 'djangocms_link', 'djangocms_picture',

'djangocms_style',

'djangocms_snippet',
'djangocms_googlemap',
'djangocms_video',
'mysite',
'storages',
'zappa_django_utils',
'aldryn_style',
'djangocms_icon',
'djangocms_bootstrap4',
'djangocms_bootstrap4.contrib.bootstrap4_alerts',
'djangocms_bootstrap4.contrib.bootstrap4_badge',
'djangocms_bootstrap4.contrib.bootstrap4_card',
'djangocms_bootstrap4.contrib.bootstrap4_carousel',
'djangocms_bootstrap4.contrib.bootstrap4_collapse',
'djangocms_bootstrap4.contrib.bootstrap4_content',
'djangocms_bootstrap4.contrib.bootstrap4_grid',
'djangocms_bootstrap4.contrib.bootstrap4_jumbotron',
'djangocms_bootstrap4.contrib.bootstrap4_link',
'djangocms_bootstrap4.contrib.bootstrap4_listgroup',
'djangocms_bootstrap4.contrib.bootstrap4_media',
'djangocms_bootstrap4.contrib.bootstrap4_picture',
'djangocms_bootstrap4.contrib.bootstrap4_tabs',
'djangocms_bootstrap4.contrib.bootstrap4_utilities',
'cmsplugin_filer_file',
'cmsplugin_filer_folder',
'cmsplugin_filer_link',
'cmsplugin_filer_image',
'cmsplugin_filer_teaser',
'cmsplugin_filer_video',

)

LANGUAGES = (

Customize this

('en', gettext('en')),

)

CMS_LANGUAGES = {

Customize this

1: [
    {
        'code': 'en',
        'name': gettext('en'),
        'redirect_on_fallback': True,
        'public': True,
        'hide_untranslated': False,
    },
],
'default': {
    'redirect_on_fallback': True,
    'public': True,
    'hide_untranslated': False,
},

}

CMS_TEMPLATES = (

Customize this

('fullwidth.html', 'Fullwidth'),
# ('sidebar_left.html', 'Sidebar Left'),
# ('sidebar_right.html', 'Sidebar Right')
('home.html', 'home'),

)

CMS_PERMISSION = True

CMS_PLACEHOLDER_CONF = {} ''' DATABASES = { 'default': { 'CONN_MAX_AGE': 0, 'ENGINE': 'django.db.backends.sqlite3', 'HOST': 'localhost', 'NAME': 'project.db', 'PASSWORD': '', 'PORT': '', 'USER': '' } } ''' ''' DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'rdzcivlp', 'USER': 'rdzcivlp', 'PASSWORD': 'DVv-uJ8d0FL7SO1sTCBaBXGgbuug0b', 'HOST': 'baasu.db.elephantsql.com', 'PORT': '5432' } } ''' ''' DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'cmscoldchip', 'USER': 'blackweb', 'PASSWORD': '$hnpiiy8gbb97ttgbi80', 'HOST': 'webinstance.cyrb6swhdrez.us-west-2.rds.amazonaws.com', 'PORT': '5432' } } ''' DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'cmscoldchip', 'USER': 'blackweb', 'PASSWORD': 'jn[mm[0i9hgbiiyif75ed7utfj', 'HOST': 'webinstance-mysql.cyrb6swhdrez.us-west-2.rds.amazonaws.com', 'PORT': '3306' } }

MIGRATION_MODULES = {

}

THUMBNAIL_PROCESSORS = ( 'easy_thumbnails.processors.colorspace', 'easy_thumbnails.processors.autocrop', 'filer.thumbnail_processors.scale_and_crop_with_subject_location', 'easy_thumbnails.processors.filters' )

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

AWS_STORAGE_BUCKET_NAME = 'zappa-djangostaticcms' AWS_ACCESS_KEY_ID = 'AKIAJ35I3SP3SUJBHJGJH' AWS_SECRET_ACCESS_KEY = 'R1RM6GwYSB1tRFNXT0RgFv4IIhih80y9080' AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME

STATICFILES_LOCATION = 'static'

STATICFILES_STORAGE = 'custom_storages.StaticStorage'

MEDIAFILES_LOCATION = 'media'

DEFAULT_FILE_STORAGE = 'custom_storages.MediaStorage'

STATIC_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN

MEDIA_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN

STATICFILES_LOCATION = 'static' STATICFILES_STORAGE = 'custom_storages.StaticStorage' STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)

MEDIAFILES_LOCATION = 'media' MEDIA_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION) DEFAULT_FILE_STORAGE = 'custom_storages.MediaStorage'

AWS_S3_REGION_NAME = 'us-west-2' MEDIA_ROOT = MEDIA_URL

AWS_DEFAULT_ACL = None AWS_QUERYSTRING_AUTH = False


- custom_storages.py

custom_storages.py

from django.conf import settings from storages.backends.s3boto3 import S3Boto3Storage

class StaticStorage(S3Boto3Storage): location = settings.STATICFILES_LOCATION

class MediaStorage(S3Boto3Storage): location = settings.MEDIAFILES_LOCATION


- urls.py

-- coding: utf-8 --

from future import absolute_import, print_function, unicode_literals

from cms.sitemaps import CMSSitemap from django.conf import settings from django.conf.urls import include, url from django.conf.urls.i18n import i18n_patterns from django.contrib import admin from django.contrib.sitemaps.views import sitemap from django.contrib.staticfiles.urls import staticfiles_urlpatterns from django.views.static import serve

admin.autodiscover()

urlpatterns = [ url(r'^sitemap.xml$', sitemap, {'sitemaps': {'cmspages': CMSSitemap}}), ]

urlpatterns += i18n_patterns( url(r'^admin/', include(admin.site.urls)), # NOQA url(r'^', include('cms.urls')), )

This is only needed when using runserver.

if settings.DEBUG: urlpatterns = [ url(r'^media/(?P.*)$', serve, {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}), ] + staticfiles_urlpatterns() + urlpatterns



## Your Environment
<!--- Include as many relevant details about the environment you experienced the bug in -->
* Zappa version used: 0.47.0
* Operating System and Python version: windows 7, Python version: 3.6.3
* Django version: 1.11.15
* django CMS version: 3.5.2
* The output of `pip freeze`:
aldryn-style==1.1.4
argcomplete==1.9.3
base58==1.0.0
boto==2.49.0
boto3==1.9.24
botocore==1.12.24
certifi==2018.10.15
cfn-flip==1.0.3
chardet==3.0.4
Click==7.0
cmsplugin-filer==1.1.3
dj-database-url==0.5.0
Django==1.11.15
django-appconf==1.0.2
django-classy-tags==0.8.0
django-cms==3.5.2
django-filer==1.3.2
django-formtools==2.1
django-mptt==0.8.7
django-polymorphic==2.0.3
django-sekizai==0.10.0
django-storages==1.7.1
django-treebeard==4.3
djangocms-admin-style==1.2.8
djangocms-attributes-field==0.3.0
djangocms-bootstrap4==1.1.1
djangocms-column==1.8.0
djangocms-file==2.0.2
djangocms-googlemap==1.1.1
djangocms-icon==1.0.0
djangocms-installer==1.0.1
djangocms-link==2.1.2
djangocms-picture==2.0.6
djangocms-snippet==2.0.0
djangocms-text-ckeditor==3.6.0
djangocms-video==2.0.4
docutils==0.14
durationpy==0.5
easy-thumbnails==2.5
future==0.16.0
hjson==3.0.1
html5lib==0.9999999
idna==2.7
jmespath==0.9.3
kappa==0.6.0
lambda-packages==0.20.0
mysqlclient==1.3.12
olefile==0.46
Pillow==5.2.0
placebo==0.8.2
psycopg2==2.7.5
python-dateutil==2.6.1
python-slugify==1.2.4
pytz==2018.5
PyYAML==3.13
requests==2.19.1
s3transfer==0.1.13
six==1.11.0
toml==0.10.0
tqdm==4.19.1
troposphere==2.3.3
tzlocal==1.5.1
Unidecode==0.4.21
urllib3==1.23
Werkzeug==0.14.1
wsgi-request-logger==0.4.6
zappa==0.47.0
zappa-django-utils==0.4.0

* Your `zappa_settings.py`: 
{
    "production": {
        "aws_region": "us-west-2",
        "django_settings": "mysite.settings",
        "profile_name": "serverless-admin",
        "project_name": "cmscoldchip",
        "runtime": "python3.6",
        "s3_bucket": "zappa-cmscoldchip-code"
    }
}
superappy commented 5 years ago

Dude you have posted a lot of critical configs related to your environment. Replace them ASAP before anyone misuses them. Be careful in future !

Abishek05 commented 5 years ago

@superappy For obvious reasons i have changed the credentials before posting. Thanks for your concern. Any insights on the issue?