简体   繁体   English

Static 文件未使用 cookiecutter-django 保存在 S3 存储桶上

[英]Static Files not saving on S3 bucket using cookiecutter-django

I'm trying to deploy my project on Heroku.我正在尝试在 Heroku 上部署我的项目。 I ran heroku run python3 manage.py collectstatic after deploying it.我在部署后运行了heroku run python3 manage.py collectstatic

I have this on config/base.py我在config/base.py上有这个

STATIC_ROOT = str(ROOT_DIR("staticfiles"))
STATIC_URL = "/static/"
STATICFILES_DIRS = [str(APPS_DIR.path("static"))]
STATICFILES_FINDERS = [
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
]

This is on the config/production.py这是在config/production.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS += ["storages"]  # noqa F405
AWS_ACCESS_KEY_ID = env("DJANGO_AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = env("DJANGO_AWS_SECRET_ACCESS_KEY")
AWS_STORAGE_BUCKET_NAME = env("DJANGO_AWS_STORAGE_BUCKET_NAME")
AWS_QUERYSTRING_AUTH = False
_AWS_EXPIRY = 60 * 60 * 24 * 7
AWS_S3_OBJECT_PARAMETERS = {
"CacheControl": f"max-age={_AWS_EXPIRY}, s-maxage={_AWS_EXPIRY}, must-revalidate"
}
AWS_DEFAULT_ACL = None
AWS_S3_REGION_NAME = env("DJANGO_AWS_S3_REGION_NAME", default=None)
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"

from storages.backends.s3boto3 import S3Boto3Storage  # noqa E402


class StaticRootS3Boto3Storage(S3Boto3Storage):
    location = "static"
    default_acl = "public-read"


class MediaRootS3Boto3Storage(S3Boto3Storage):
    location = "media"
    file_overwrite = False

DEFAULT_FILE_STORAGE = "config.settings.production.MediaRootS3Boto3Storage"
MEDIA_URL = f"https://{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/media/"

These are my heroku env variables这些是我的 heroku 环境变量在此处输入图像描述

I generated this using cookiecutter-django.我使用 cookiecutter-django 生成了这个。 Everything works fine on my localhost but when I deploy it to heroku, it doesn't save the static files.在我的本地主机上一切正常,但是当我将它部署到 heroku 时,它不会保存 static 文件。

Instead of uploading the assets to S3, it could be easier to use whitenoise to serve the static files.与其将资产上传到 S3,不如使用白噪声来提供 static 文件可能更容易。 Basically whitenoise allows you to serve the static files from your django app instead of somewhere else.基本上,白噪声允许您从 django 应用程序而不是其他地方提供 static 文件。

Install whitenoise with pip install.使用 pip 安装白噪声。 pip install whitenoise . pip install whitenoise

You'll need to include whitenoise as a middleware.您需要将白噪声作为中间件包括在内。

MIDDLEWARE = [
  'django.middleware.security.SecurityMiddleware',
  'whitenoise.middleware.WhiteNoiseMiddleware',
  # ...
]

To use whitenoise in development mode.在开发模式下使用白噪声。 You need an installed app.您需要一个已安装的应用程序。

INSTALLED_APPS = [
    'whitenoise.runserver_nostatic',
    'django.contrib.staticfiles',
    # ...
]

That's about it.就是这样。 You can read more about in the documentation.您可以在文档中阅读更多信息。 http://whitenoise.evans.io/en/stable/django.html http://whitenoise.evans.io/en/stable/django.html

For more performance you can configure it to use a CDN, but if it's just a small site, that's not necessary.为了获得更高的性能,您可以将其配置为使用 CDN,但如果它只是一个小型站点,则没有必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM