我想从Amazon S3和本地服务器安装静态文件?
我也不知道如何设置MEDIA_URL
STATIC_ROOT
和MEDIA_ROOT
上下文:
我使用Amazon S3
从django-boto
提供我的静态文件,我的settings/base.py
是:
STATICFILES_LOCATION = 'assets'
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'
我的custom_storages.py
文件内容为:
from django.conf import settings
# from storages.backends.s3boto3 import S3Boto3Storage
from storages.backends.s3boto import S3BotoStorage
class StaticStorage(S3BotoStorage):
location = settings.STATICFILES_LOCATION
class MediaStorage(S3BotoStorage):
location = settings.MEDIAFILES_LOCATION
所有这一切都很好。
当我执行collectstatic
时,我的静态文件将被上传到我在Amazon S3中的存储桶。
我遇到的问题是,每次我在css
或js
文件中进行更改时,我都需要执行collectstatic
命令。
如何设置我的项目(设置)以便一起在本地服务器中提供来自S3和我的django的静态文件?
我有一个settings/development.py
文件,其中我将覆盖以下设置:
STATIC_URL = '/assets/'
STATICFILES_LOCATION = 'assets'
MEDIAFILES_LOCATION = 'media/'
MEDIA_URL = MEDIAFILES_LOCATION
STATIC_ROOT = os.path.join(BASE_DIR, "assets")
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
我的urls.py
主文件我有这个条件:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)